UITableView 自定义多选

前言

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

return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;

改为

return UITableViewCellEditingStyleDelete & UITableViewCellEditingStyleInsert;

可以实现自定义的多选操作,这次就来实现一下。

第一步:

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

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

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


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


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

第三步:

处理选中/取消选中逻辑

Cell.h:

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中添加按钮:

- (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方法接收回调并添加到选择的行数组中或从数组删除

- (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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值