在上一篇文章中介紹了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
如上圖所示,當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]; } }