服务器之家

服务器之家 > 正文

iOS 多选删除功能附tableViewTips及单选删除

时间:2021-03-16 15:55     来源/作者:王隆帅

一、前言

这次分享并记录一下tableview的多选删除,并额外记录一下单选删除及tableview的设置小技巧。

二、想要实现的效果图如下:

1、先上原图

iOS 多选删除功能附tableViewTips及单选删除

2、然后编辑图如下:

iOS 多选删除功能附tableViewTips及单选删除

3、编辑步骤:

点击右上角按钮编辑,界面呈现编辑状态底部删除按钮弹出

选择删除cell项,点击右下角删除可删除

点击右上角,退出编辑状态,底部删除按钮退出界面

三、多选删除核心代码

1、设置允许tableview编辑状态下允许多选

?
1
_maintableview.allowsmultipleselectionduringediting = yes;

2、将cell设置为可选择的风格(下面代码是在自定义cell内部)

?
1
self.selectionstyle = uitableviewcellselectionstyledefault;

说明:因为自认为系统的选中状态很难看,所以在cell的基类里已经把 selectionstyle 设置为none,但是想要多选必须将其打开,大家切记。

3、不喜欢系统的选中状态,可更改选中状态的背景(下面也是在自定义cell内部)

?
1
2
3
uiview *view = [[uiview alloc] init];
view.backgroundcolor = uicolorfromrgb(0xf6f6f6);
self.selectedbackgroundview = view;

4、右上角点击事件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[self.viewmodel.rightviewmodel.clicksubject subscribenext:^(id x) {
   @strongify(self);
   if (self.maintableview.editing) {
     self.viewmodel.rightviewmodel.title = @"编辑";
     [uiview animatewithduration:0.5 animations:^{
       [self.maintableview mas_remakeconstraints:^(masconstraintmaker *make) {
         @strongify(self);
         make.edges.equalto(self);
       }];
     }];
   } else {
     self.viewmodel.rightviewmodel.title = @"确定";
     [uiview animatewithduration:0.5 animations:^{
       [self.maintableview mas_remakeconstraints:^(masconstraintmaker *make) {
         @strongify(self);
         make.left.right.top.equalto(self);
         make.bottom.equalto(-50);
       }];
     }];
   }
   [self.maintableview setediting:!self.maintableview.editing animated:yes];
 }];

5、右下角删除事件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[[[self.deletebtn rac_signalforcontrolevents:uicontroleventtouchupinside] takeuntil:self.rac_willdeallocsignal] subscribenext:^(id x) {
    @strongify(self);
   nsmutablearray *deletearray = [nsmutablearray array];
   for (nsindexpath *indexpath in self.maintableview.indexpathsforselectedrows) {
     [deletearray addobject:self.viewmodel.dataarray[indexpath.row]];
   }
   nsmutablearray *currentarray = self.viewmodel.dataarray;
   [currentarray removeobjectsinarray:deletearray];
   self.viewmodel.dataarray = currentarray;
   [self.maintableview deleterowsatindexpaths:self.maintableview.indexpathsforselectedrows withrowanimation:uitableviewrowanimationleft];//删除对应数据的cell
   dispatch_time_t delaytime = dispatch_time(dispatch_time_now, (int64_t)(1.0 * nsec_per_sec));
   dispatch_after(delaytime, dispatch_get_main_queue(), ^{
     @strongify(self);
     [self.maintableview reloaddata];
   });
 }];

四、单个删除(分为系统左滑,和点击cell上删除按钮两种)

1、系统左滑

?
1
2
3
4
5
6
7
8
9
10
#pragma mark - delete
- (uitableviewcelleditingstyle)tableview:(uitableview *)tableview editingstyleforrowatindexpath:(nsindexpath *)indexpath {
  return uitableviewcelleditingstyledelete;
}
- (nsstring *)tableview:(uitableview *)tableview titlefordeleteconfirmationbuttonforrowatindexpath:(nsindexpath *)indexpath {
  return @"删除此经验";
}
- (void)tableview:(uitableview *)tableview commiteditingstyle:(uitableviewcelleditingstyle)editingstyle forrowatindexpath:(nsindexpath *)indexpath {
  [self.viewmodel.deletecommand execute:indexpath];
}

说明:删除操作数据及ui刷新和多选是一致的,就不上代码了,这里只需注意左滑需要遵循的系统代理就行。

2、点击cell删除

与系统左滑删除的不同仅仅是手动触发删除事件而已。

?
1
2
3
[[[self.deletebtn rac_signalforcontrolevents:uicontroleventtouchupinside] takeuntil:self.rac_prepareforreusesignal] subscribenext:^(id x) {
  [viewmodel.deletecommand execute:nil];
}];

单个删除的操作数据和ui刷新也上下代码吧!(虽然有些重复o(╯□╰)o)

?
1
2
3
4
5
6
7
8
9
10
11
12
[[self.viewmodel.deletesubject takeuntil:self.rac_willdeallocsignal] subscribenext:^(nsindexpath *indexpath) {
   @strongify(self);
   if (self.viewmodel.dataarray.count > indexpath.row) {
     [self.viewmodel.dataarray removeobjectatindex:indexpath.row]; //删除数组里的数据
     [self.maintableview deleterowsatindexpaths:[nsmutablearray arraywithobject:indexpath] withrowanimation:uitableviewrowanimationleft];//删除对应数据的cell
     dispatch_time_t delaytime = dispatch_time(dispatch_time_now, (int64_t)(1.0 * nsec_per_sec));
     dispatch_after(delaytime, dispatch_get_main_queue(), ^{
        @strongify(self);
        [self.maintableview reloaddata];
     });
   }
 }];

五、tableview的一些tips(不常用的,或没注意的)

1、设置tableview可不可以选中(防止cell重复点击也可以利用这条特性)

?
1
self.tableview.allowsselection = no;

2、允许tableview多选

?
1
self.tableview.allowsmultipleselection = yes;

3、编辑模式下是否可以选中

?
1
self.tableview.allowsselectionduringediting = no;

4、编辑模式下是否可以多选

?
1
self.tableview.allowsmultipleselectionduringediting = yes;

5、获取被选中的所有行

?
1
[self.tableview indexpathsforselectedrows]

6、获取当前可见的行

?
1
[self.tableview indexpathsforvisiblerows];

7、 改变uitableviewcell选中时背景色

?
1
cell.selectedbackgroundview.backgroundcolor

8、自定义uitableviewcell选中时背景

?
1
cell.selectedbackgroundview

9、自定义uitableviewcell选中时系统label字体颜色

?
1
cell.textlabel.highlightedtextcolor

10、设置tableviewcell间的分割线的颜色

?
1
[thetableview setseparatorcolor:[uicolor xxxx ]];

11、pop返回table时,cell自动取消选中状态(在viewwillappear中添加如下代码)

?
1
[self.tableview deselectrowatindexpath:[self.tableview indexpathforselectedrow] animated:yes];

12、点击后,过段时间cell自动取消选中

?
1
2
3
4
5
6
7
- (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath {
  //消除cell选择痕迹
  [self performselector:@selector(deselect) withobject:nil afterdelay:0.5f];
}
- (void)deselect {
  [self.tableview deselectrowatindexpath:[self.tableview indexpathforselectedrow] animated:yes];
}

以上所述是小编给大家介绍的anios 多选删除功能附tableviewtips及单选删除,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:http://www.jianshu.com/p/cd5771d66349#

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部