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];
}
}

最终效果图]
专注iOS、Golang开发。
技术博客:http://xiaopin.cnblogs.com
分类:
iOS 常用小知识点
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?