首先我们先来看一下效果图:
接下来,我们看一下实现方式,在ViewDidLoad中将UITableView的颜色去掉,然后就是定义UITableViewDataSource和UITableViewDelegate了,看一下代码吧:
这里没什么特别的,只是在didSelectRowAtIndexPath中写了一句判断语句
并实现多选方法中中协议来关掉当前页面,并将选中的数据传出
这里有很重要的一点就是委托方法的应用,如果前面一个页面需要后面页面的数据,就要想到用委托来实现回调,其实只要掌握了委托一些页面间的传值也就不难了,这篇文章只是给大家做个介绍,但如何做出自己想要的效果还是需要多多练习和学习的。谢谢
一般大家都是用UITableView自己的编辑模式来实现CheckBox的,这里我们用自定义Cell和两张图片来实现,一张是未选中,一张是选中的图片
好了,我们首先来看一下代码:首先在Cell中定义了三个控件,两个UILabel和一个UIImageView
- #import <UIKit/UIKit.h>
- @interface MultiChoceCell : UITableViewCell
- @property (strong, nonatomic) IBOutlet UILabel *nameLabel;
- @property (strong, nonatomic) IBOutlet UILabel *departLable;
- @property (strong, nonatomic) IBOutlet UIImageView *checkBox;
- @end
好了,这个相信各位学过UITableViewCell的同学都应该知道,接下来,我们就来写最主要的ViewController了
在头文件.h文件里,我们首先定义了一个协议专门用来做回调的,还定义了一个选项数组和选中的数组,还有一个UITableView,我个人喜欢在ViewController里套上UITableView,因为可以改变TableView的大小。
- #import <UIKit/UIKit.h>
- @class MultiChoceViewController;
- @protocol MultiChoceDelegate <NSObject>
- @required
- -(void)MultiChoceSelectArray:(NSArray *)array ViewController:(MultiChoceViewController *)controller;
- @end
- @interface MultiChoceViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
- @property(nonatomic, strong)NSArray *itemArray;
- @property(nonatomic, strong)NSMutableArray *selectArray;
- @property (strong, nonatomic) IBOutlet UITableView *mTableView;
- @property (nonatomic, strong) id<MultiChoceDelegate> delegate;
- - (IBAction)backAction:(id)sender;
- - (IBAction)okAction:(id)sender;
- @end
- #import "MultiChoceViewController.h"
- #import "MultiChoceCell.h"
- @interface MultiChoceViewController ()
- @end
- @implementation MultiChoceViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- [_mTableView setBackgroundColor:[UIColor clearColor]];
- // Do any additional setup after loading the view from its nib.
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- #pragma mark UITableViewDataSource
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
- return [_itemArray count];
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
- static NSString *identifier = @"itemCell";
- MultiChoceCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
- if (cell == nil) {
- NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"MultiChoceCell" owner:self options:nil];
- cell = [array objectAtIndex:0];
- }
- NSDictionary *dict = [_itemArray objectAtIndex:indexPath.row];
- cell.nameLabel.text = [dict objectForKey:@"UserName"];
- cell.departLable.text = [dict objectForKey:@"DepartMent"];
- if ([_selectArray containsObject:dict]) {
- cell.checkBox.image = [UIImage imageNamed:@"checked.png"];
- }
- return cell;
- }
- - (IBAction)backAction:(id)sender {
- [self.navigationController popViewControllerAnimated:YES];
- }
- - (IBAction)okAction:(id)sender {
- [_delegate MultiChoceSelectArray:_selectArray ViewController:self];
- }
- #pragma mark UITableViewDelegate
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
- NSDictionary *selectDict = [_itemArray objectAtIndex:indexPath.row];
- //判断数据是否在选择列表中
- if ([_selectArray containsObject:selectDict]) {
- [_selectArray removeObject:selectDict];
- }else{
- [_selectArray addObject:selectDict];
- }
- [_mTableView reloadData];
- }
- @end
在调用的页面,我们把itemArray和selectArray传入
- MultiChoceViewController *controlller = [[MultiChoceViewController alloc] initWithNibName:@"MultiChoceViewController" bundle:nil];
- controlller.delegate = self;
- controlller.itemArray = userArray;
- controlller.selectArray = selectArray;
- [self.navigationController pushViewController:controlller animated:YES];
- #pragma mark MultiChoceDelegate
- -(void)MultiChoceSelectArray:(NSMutableArray *)array ViewController:(MultiChoceViewController *)controller{
- selectArray = array;
- [self.navigationController popViewControllerAnimated:YES];
- }