iPhone實現自定義多選列表

好久沒更新博客了,今天寫了一個自定義的多選列表,可以跟愛學習的各位進行分享,首先我們先來看一下效果圖:

一般大家都是用UITableView自己的編輯模式來實現CheckBox的,這裏我們用自定義Cell和兩張圖片來實現,一張是未選中,一張是選中的圖片

好了,我們首先來看一下代碼:首先在Cell中定義了三個控件,兩個UILabel和一個UIImageView

[java] view plaincopy
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface MultiChoceCell : UITableViewCell  
  4. @property (strong, nonatomic) IBOutlet UILabel *nameLabel;  
  5. @property (strong, nonatomic) IBOutlet UILabel *departLable;  
  6. @property (strong, nonatomic) IBOutlet UIImageView *checkBox;  
  7.   
  8. @end  

好了,這個相信各位學過UITableViewCell的同學都應該知道,接下來,我們就來寫最主要的ViewController了

在頭文件.h文件裏,我們首先定義了一個協議專門用來做回調的,還定義了一個選項數組和選中的數組,還有一個UITableView,我個人喜歡在ViewController裏套上UITableView,因爲可以改變TableView的大小。

[java] view plaincopy
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @class MultiChoceViewController;  
  4.   
  5. @protocol MultiChoceDelegate <NSObject>  
  6.   
  7. @required  
  8.   
  9. -(void)MultiChoceSelectArray:(NSArray *)array ViewController:(MultiChoceViewController *)controller;  
  10.   
  11. @end  
  12.   
  13.   
  14. @interface MultiChoceViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>  
  15.   
  16. @property(nonatomic, strong)NSArray *itemArray;  
  17. @property(nonatomic, strong)NSMutableArray *selectArray;  
  18. @property (strong, nonatomic) IBOutlet UITableView *mTableView;  
  19. @property (nonatomic, strong) id<MultiChoceDelegate> delegate;  
  20.   
  21. - (IBAction)backAction:(id)sender;  
  22. - (IBAction)okAction:(id)sender;  
  23.   
  24. @end  
接下來,我們看一下實現方式,在ViewDidLoad中將UITableView的顏色去掉,然後就是定義UITableViewDataSource和UITableViewDelegate了,看一下代碼吧:

[java] view plaincopy
  1. #import "MultiChoceViewController.h"  
  2. #import "MultiChoceCell.h"  
  3.   
  4. @interface MultiChoceViewController ()  
  5.   
  6. @end  
  7.   
  8. @implementation MultiChoceViewController  
  9.   
  10. - (void)viewDidLoad  
  11. {  
  12.     [super viewDidLoad];  
  13.       
  14.     [_mTableView setBackgroundColor:[UIColor clearColor]];  
  15.       
  16.     // Do any additional setup after loading the view from its nib.  
  17. }  
  18.   
  19. - (void)didReceiveMemoryWarning  
  20. {  
  21.     [super didReceiveMemoryWarning];  
  22.     // Dispose of any resources that can be recreated.  
  23. }  
  24.   
  25. #pragma mark UITableViewDataSource  
  26. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{  
  27.       
  28.     return [_itemArray count];  
  29. }  
  30.   
  31.   
  32. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  
  33.       
  34.     static NSString *identifier = @"itemCell";  
  35.       
  36.     MultiChoceCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];  
  37.       
  38.     if (cell == nil) {  
  39.         NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"MultiChoceCell" owner:self options:nil];  
  40.           
  41.         cell = [array objectAtIndex:0];  
  42.     }  
  43.       
  44.     NSDictionary *dict = [_itemArray objectAtIndex:indexPath.row];  
  45.       
  46.     cell.nameLabel.text = [dict objectForKey:@"UserName"];  
  47.     cell.departLable.text = [dict objectForKey:@"DepartMent"];  
  48.       
  49.     if ([_selectArray containsObject:dict]) {  
  50.         cell.checkBox.image = [UIImage imageNamed:@"checked.png"];  
  51.     }  
  52.       
  53.     return cell;  
  54.   
  55. }  
  56.   
  57. - (IBAction)backAction:(id)sender {  
  58.       
  59.     [self.navigationController popViewControllerAnimated:YES];  
  60. }  
  61.   
  62. - (IBAction)okAction:(id)sender {  
  63.       
  64.     [_delegate MultiChoceSelectArray:_selectArray ViewController:self];  
  65.       
  66. }  
  67.   
  68. #pragma mark UITableViewDelegate  
  69. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{  
  70.       
  71.     NSDictionary *selectDict = [_itemArray objectAtIndex:indexPath.row];  
[java] view plaincopy
  1.     //判斷數據是否在選擇列表中  
  2.     if ([_selectArray containsObject:selectDict]) {  
  3.         [_selectArray removeObject:selectDict];  
  4.     }else{  
  5.         [_selectArray addObject:selectDict];  
  6.     }  
  7.       
  8.     [_mTableView reloadData];  
  9. }  
  10. @end  
這裏沒什麼特別的,只是在didSelectRowAtIndexPath中寫了一句判斷語句

在調用的頁面,我們把itemArray和selectArray傳入

[java] view plaincopy
  1. MultiChoceViewController *controlller = [[MultiChoceViewController alloc] initWithNibName:@"MultiChoceViewController" bundle:nil];  
  2.     controlller.delegate = self;  
  3.       
  4.     controlller.itemArray = userArray;  
  5.     controlller.selectArray = selectArray;  
  6.       
  7.     [self.navigationController pushViewController:controlller animated:YES];  
並實現多選方法中中協議來關掉當前頁面,並將選中的數據傳出

[java] view plaincopy
  1. #pragma mark MultiChoceDelegate  
  2. -(void)MultiChoceSelectArray:(NSMutableArray *)array ViewController:(MultiChoceViewController *)controller{  
  3.       
  4.     selectArray = array;  
  5.       
  6.     [self.navigationController popViewControllerAnimated:YES];  
  7.       
  8. }  
這裏有很重要的一點就是委託方法的應用,如果前面一個頁面需要後面頁面的數據,就要想到用委託來實現回調,其實只要掌握了委託一些頁面間的傳值也就不難了,這篇文章只是給大家做個介紹,但如何做出自己想要的效果還是需要多多練習和學習的。謝謝
發佈了214 篇原創文章 · 獲贊 5 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章