iOS中UITableView的编辑状态多选

来源:互联网 发布:sql server培训费用 编辑:程序博客网 时间:2024/05/18 16:57

开发工作中经常遇到列表编辑多选功能,UITableView其实自带的有多选功能,使用起来方便,不需要自己做选中状态,效果:

模拟器截图

获取多选cell的位置信息:

```在  didSelectRowAtIndexPath:方法里获取并打印选中cell的位置信息:```- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {NSArray*selectCells = [tableView indexPathsForSelectedRows];NSLog(@"------%@",selectCells);}<div class="se-preview-section-delimiter"></div>

打印结果:

" {length = 2, path = 0 - 2}"," {length = 2, path = 0 - 4}"," {length = 2, path = 0 - 7}"," {length = 2, path = 0 - 10}"," {length = 2, path = 0 - 8}"," {length = 2, path = 0 - 6}")<div class="se-preview-section-delimiter"></div>

数组结果里是按照选中的顺序存储的数据。

然后是全选和全不选的操作,以下都是在单section情况下:
全选:

遍历一遍数据源 然后选中每一条:

- (void)allSelectAction:(UIButton*)sender {[self.dataSourceenumerateObjectsUsingBlock:^(id_Nonnullobj,NSUIntegeridx,BOOL*_Nonnullstop) {[self.tableViewselectRowAtIndexPath:[NSIndexPathindexPathForRow:idxinSection:0]animated:NOscrollPosition:UITableViewScrollPositionNone];}];}<div class="se-preview-section-delimiter"></div>

全选

全不选:

全不选,reload就可以变为全不选状态了,如果不想reload这样简单粗暴的,也可以取出当前选中的indexpath数组,遍历反选也可以。

- (void)allUnSelectAction:(UIButton*)sender {[[self.tableViewindexPathsForSelectedRows]enumerateObjectsUsingBlock:^(NSIndexPath*_Nonnullobj,NSUIntegeridx,BOOL*_Nonnullstop) {[self.tableViewdeselectRowAtIndexPath:objanimated:NO];}];}

取消选择/全不选

引用文章:www.jianshu.com/p/a6e4cb42dd03
UITableView相关方法使用详细介绍:

http://www.jianshu.com/p/7680f1432fee

![全选](http://upload-images.jianshu.io/upload_images/2215798-b165034b8805c5cc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)全不选:全不选,reload就可以变为全不选状态了,如果不想reload这样简单粗暴的,也可以取出当前选中的indexpath数组,遍历反选也可以。```//全不选按钮事件- (void)allUnSelectAction:(UIButton*)sender {[[self.tableViewindexPathsForSelectedRows]enumerateObjectsUsingBlock:^(NSIndexPath*_Nonnullobj,NSUIntegeridx,BOOL*_Nonnullstop) {[self.tableViewdeselectRowAtIndexPath:objanimated:NO];}];}

取消选择/全不选

引用文章地址

UITableView相关方法使用详细介绍

0 0
原创粉丝点击