前言
在上一篇文章中介紹了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上,無法實現想要的效果,請看下圖:
如上圖所示,當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];
}
}