iPhone实现自定义多选列表

来源:互联网 发布:淘宝怎么删除下架宝贝 编辑:程序博客网 时间:2024/05/23 15:54

好久没更新博客了,今天写了一个自定义的多选列表,可以跟爱学习的各位进行分享,首先我们先来看一下效果图:

一般大家都是用UITableView自己的编辑模式来实现CheckBox的,这里我们用自定义Cell和两张图片来实现,一张是未选中,一张是选中的图片

好了,我们首先来看一下代码:首先在Cell中定义了三个控件,两个UILabel和一个UIImageView

[java] view plain copy
  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 plain copy
  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 plain copy
  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 plain copy
  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 plain copy
  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 plain copy
  1. #pragma mark MultiChoceDelegate  
  2. -(void)MultiChoceSelectArray:(NSMutableArray *)array ViewController:(MultiChoceViewController *)controller{  
  3.       
  4.     selectArray = array;  
  5.       
  6.     [self.navigationController popViewControllerAnimated:YES];  
  7.       
  8. }  
这里有很重要的一点就是委托方法的应用,如果前面一个页面需要后面页面的数据,就要想到用委托来实现回调,其实只要掌握了委托一些页面间的传值也就不难了,这篇文章只是给大家做个介绍,但如何做出自己想要的效果还是需要多多练习和学习的。谢谢
0 0
原创粉丝点击