UITableView 自定义多选

前言

在上一篇文章中介绍了UITableView的多选操做,有提到将数组

return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;

改成ide

return UITableViewCellEditingStyleDelete & UITableViewCellEditingStyleInsert;

能够实现自定义的多选操做,此次就来实现一下。学习

第一步:

自定义一个Cell类:UDTableViewCell,在nib中设置好重用标识,从新TableView注册这个nib Cell :ui

[self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([UDTableViewCell class]) bundle:[NSBundle mainBundle]] forCellReuseIdentifier:reuseIdentifier];

第二步:

在Cell中添加一个选择按钮,若是按钮直接添加到Cell的contentview或Cell上,没法实现想要的效果,请看下图:atom


iOS Simulator Screen Shot 2015年9月7日 上午10.30.53.png


如上图所示,当UITableView处于多选状态的时候,整个Cell的contentview向右移,露出后面的backgroundView,图中蓝色部分就是backgroundView,咱们须要将选择按钮添加到backgroundView上,编辑的时候选择按钮天然就显示出来了。spa

第三步:

处理选中/取消选中逻辑3d

Cell.h:code

typedef void(^CustomSelectBlock)(BOOL selected, NSInteger row); @interface UDTableViewCell : UITableViewCell @property (nonatomic, assign) NSInteger row; @property (nonatomic, strong) UIButton *btnSelect; @property (nonatomic, getter=isCustomSelected) BOOL customSelected; @property (nonatomic, copy) CustomSelectBlock customSelectedBlock;

Cell中添加按钮:orm

- (void)awakeFromNib { UIView *backgroundView = [[UIView alloc] initWithFrame:self.contentView.bounds]; backgroundView.backgroundColor = [UIColor clearColor]; self.backgroundView = backgroundView; self.contentView.backgroundColor = [UIColor greenColor]; self.btnSelect = [UIButton buttonWithType:UIButtonTypeCustom]; self.btnSelect.frame = CGRectMake( 15, 2, selectButtonSize, selectButtonSize); self.btnSelect.backgroundColor = [UIColor clearColor]; [backgroundView addSubview:self.btnSelect]; [self.btnSelect setTitle:@"⭕️" forState:UIControlStateNormal]; self.btnSelect.titleLabel.font = [UIFont systemFontOfSize:20]; self.btnSelect.contentEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5); [self.btnSelect addTarget:self action:@selector(selectAction:) forControlEvents:UIControlEventTouchUpInside]; }
- (IBAction)selectAction:(id)sender{ _customSelected = !_customSelected; if ([self.btnSelect.titleLabel.text isEqualToString:@"⭕️"]) { [self.btnSelect setTitle:@"🔴" forState:UIControlStateNormal]; }else{ [self.btnSelect setTitle:@"⭕️" forState:UIControlStateNormal]; } !_customSelectedBlock ?: _customSelectedBlock(_customSelected, _row); }

VC中datasouce方法接收回调并添加到选择的行数组中或从数组删除blog

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UDTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier forIndexPath:indexPath]; cell.textLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row]; cell.row = indexPath.row; __weak typeof(self) weakSelf = self; cell.customSelectedBlock = ^ (BOOL selected, NSInteger row) { if (selected) { [weakSelf.selectedRows addObject:@(row)]; }else { [weakSelf.selectedRows removeObject:@(row)]; } }; return cell; }

由于Cell的重用机制,因此当Cell滑进屏幕时会重用在重用队列中的Cell,致使Cell自定义选中状态不定。若是处理?:在Cell将要显示出来的时候判断一下作处理。

- (void)tableView:(UITableView *)tableView willDisplayCell:(UDTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { if ([self.selectedRows containsObject:@(cell.row)]) { [cell.btnSelect setTitle:@"🔴" forState:UIControlStateNormal]; }else { [cell.btnSelect setTitle:@"⭕️" forState:UIControlStateNormal]; } }

最终效果图]
 
收藏学习:转自:http://www.jianshu.com/p/4863580bf627
相关文章
相关标签/搜索