iOS tableviewCell的多行选择删除和全选删除_weixin_34260991的博客-程序员宅基地

技术标签: python  markdown  

 

 

记录一下项目中遇到的tableviewCell多行选择删除和全选删除。

1240

1240

1240

1240

效果图如上图

1 首先 创建数组

2 创建tableview 

self.tableView.editing = NO;//默认tableview的editing 是不开启的

3 全选和多选 删除按钮

//选择按钮

UIButton *selectedBtn = [UIButton buttonWithType:UIButtonTypeSystem];

selectedBtn.frame = CGRectMake(0, 0, 60, 30);

[selectedBtn setTitle:@"选择" forState:UIControlStateNormal];

[selectedBtn addTarget:self action:@selector(selectedBtn:) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *selectItem = [[UIBarButtonItem alloc] initWithCustomView:selectedBtn];

self.navigationItem.rightBarButtonItem =selectItem;

//        全选

_selectAllBtn = [UIButton buttonWithType:UIButtonTypeSystem];

_selectAllBtn.frame = CGRectMake(0, 0, 60, 30);

[_selectAllBtn setTitle:@"全选" forState:UIControlStateNormal];

[_selectAllBtn addTarget:self action:@selector(selectAllBtnClick:) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:_selectAllBtn];

self.navigationItem.leftBarButtonItem = leftItem;

_selectAllBtn.hidden = YES;

_baseView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height- 60, self.view.frame.size.width, 60)];

_baseView.backgroundColor = [UIColor grayColor];

[self.view addSubview:_baseView];

//删除按钮

_deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];

_deleteBtn.backgroundColor = [UIColor redColor];

[_deleteBtn setTitle:@"删除" forState:UIControlStateNormal];

_deleteBtn.frame = CGRectMake(0, 0, self.view.frame.size.width, 60);

[_deleteBtn addTarget:self action:@selector(deleteClick:) forControlEvents:UIControlEventTouchUpInside];

[_baseView addSubview:_deleteBtn];

4 tableview的delegate和datasource

//是否可以编辑  默认的时YES

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{

return YES;

}

//选择编辑的方式,按照选择的方式对表进行处理

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

if (editingStyle == UITableViewCellEditingStyleDelete) {//删除

//真正项目中做删除

//1.将表中的cell删除

//2.将本地的数组中数据删除

//3.最后将服务器端的数据删除

}

}

//选择你要对表进行处理的方式  默认是删除方式

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;

}

******//选中时将选中行的在self.dataArray 中的数据添加到删除数组self.deleteArr中

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

[self.deleteArr addObject:[self.dataArray objectAtIndex:indexPath.row]];

}

******//取消选中时 将存放在self.deleteArr中的数据移除

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath  {

[self.deleteArr removeObject:[self.dataArray objectAtIndex:indexPath.row]];

}

实现完tableview的代理方法 下面处理各个按钮的响应事件

1 首先是选择按钮的响应事件 在按钮事件里面要有self.tableView.allowsMultipleSelectionDuringEditing = YES; 允许支持同时选中多行

然后在点击的时候让tableview.editing = !tableview.editing 点击此按钮可切换tableview的编辑状态

- (void)selectedBtn:(UIButton *)button {

//支持同时选中多行

self.tableView.allowsMultipleSelectionDuringEditing = YES;

self.tableView.editing = !self.tableView.editing;

if (self.tableView.editing) {

_selectAllBtn.hidden = NO;

[button setTitle:@"完成" forState:UIControlStateNormal];

}else{

_selectAllBtn.hidden = YES;

[button setTitle:@"删除" forState:UIControlStateNormal];

}

}

 

2 全选按钮的响应事件

点击全选按钮时 要在这里选中所有的cell 我在网上看到很多资料都是选中当前可见的cell 我们项目要求是全部cell ,所以在这里我这样去做 在self.dataArray里面遍历, 然后找到对应的一共多少行, 获取索引值 indexPath,tableview有系统方法 [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop]; 选择全部的cell  此时在这个全选方法中将数据源self.dataArray的所有数据全部添加到self.deleteArr (存储删除数据的数组中)

//全选

- (void)selectAllBtnClick:(UIButton *)button {

//    [self.tableView reloadData];

for (int i = 0; i < self.dataArray.count; i ++) {

NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];

[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];

[self.deleteArr addObjectsFromArray:self.dataArray];

}

NSLog(@"self.deleteArr:%@", self.deleteArr);

}

3 删除按钮的处理事件

无论是多行删除还是全部删除的数据对tableview的操作都是走这个delete方法的。在方法中判断当前的tableview是否处于编辑状态。然后再执行删除操作,关键点就是将数据源self.dataArray中的要删除的数据移除,之前我们的多选或者全选已经将我们要删除的数据存储在self.deleteArr中了 ,所以在这里我们用[self.dataArray removeObjectsInArray:self.deleteArr];这个方法操作 然后刷新tableview。至此就可以实现功能了。

 

- (void)deleteClick:(UIButton *) button {

if (self.tableView.editing) {

//删除

[self.dataArray removeObjectsInArray:self.deleteArr];

[self.tableView reloadData];

}

else return;

}

 

总结 :这里面一共有以下几个点 

1 在多选删除时 在didSelectRowAtIndexPath这个方法中,根据cell所在行的索引值将此行的数据存到self.deleteArr中 [self.deleteArr addObject:[self.dataArray objectAtIndex:indexPath.row]];

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

}

当取消删除时,要将self.deleteArr中的数据移除,不然会造成 (你先选中一行 然后取消选中 但是当你点击删除按钮时,这行cell还是会被删除)

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath  {

}

2 在全选时。将self.dataArr 中的全部数据都赋值给self.deleteArr

NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];

[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];

[self.deleteArr addObjectsFromArray:self.dataArray];

3 执行删除操作时,将self.dataArr中包含self.deleteArr的数据移除

self.dataArray removeObjectsInArray:self.deleteArr];

[self.tableView reloadData];

 

后记:还有一个在cell中添加长按手势 使tableview进入编辑状态

在-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 方法中 为cell添加长按手势

UILongPressGestureRecognizer *longPressed = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressedAct:)];

longPressed.minimumPressDuration = 1;

[cell.contentView addGestureRecognizer:longPressed];

 

实现长按手势的响应方法:

-(void)longPressedAct:(UILongPressGestureRecognizer *)gesture

{

if(gesture.state == UIGestureRecognizerStateBegan)

{

CGPoint point = [gesture locationInView:self.tableView];

NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint:point];

if(indexPath == nil) return ;

//add your code here

self.tableView.editing = YES;

}

}

Markdown模式链接

转载于:https://my.oschina.net/mexiaobai1315/blog/783833

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_34260991/article/details/92681210

智能推荐

2019-12-8数组练习(函数递归)_weixin_45850328的博客-程序员宅基地

1、计算下面公式并输出:函数原型:float fun(int n)//函数原型指的是函数首部\tiny S=1+(1+\sqrt{2})+(1+\sqrt{2}+\sqrt{3})+…+(1+\sqrt{2}+\sqrt{3}+…+\sqrt{n})#include &lt;stdio.h&gt;#include&lt;math.h&gt;double fun(int n){doub...

前端设置前端代理解决跨域_DB&A的博客-程序员宅基地

项目使用vue脚手架3搭建1,提到前端代理,就会想到proxy,下图是根目录config下面的index.js,在proxyTable里面添加内容,见下图2在config里面添加一个API3最后的话,就是设置axios默认导出的baseURL;process.env.API就是当前环境的API;因为axios在vue原型里面挂载了的,直接设置axios的默认baseURL。这样就可以直接跨域问题了,看起来好像比较简单,但是做之前研究半天。0.0...

mysql数据库表生成实体类_玉&心的博客-程序员宅基地

  参考了http://www.oschina.net/code/snippet_857839_15477mysql数据库表生成实体类代码如下,稍微改造了一下:package com.chinamobile.pumpValve.util;import java.io.File;import java.io.FileWriter;import java.io.IOExce...

刷ccf学到的知识_YK海蓝时见鲸的博客-程序员宅基地

201903:1.需要存储较多的数时,可以使用静态数组,即作为全局变量声明,因为堆提供的空间更大,全局变量存储在堆中,局部变量存储在栈中;2.判断数n是奇数还是偶数的方法:(1)n%2!=0为奇数,0为偶数。(2)从二进制(补码)的角度:n&amp;10为偶数,1为奇数。a&amp;b是a的补码与b的补码按位进行与运算,也就是乘。与1进行与运算也就是将前几位清0,只留补码最后一位,奇数补码最后...

在CentOS5.8下从源码安装xen-3.4.4.tar.gz_langeldep的博客-程序员宅基地

从官网下载到 xen-3.4.4.tar.gz ,解压缩后, 直接make ;发生如下错误:select-repository: Searching `.:..' for linux-2.6.18-xen.hgselect-repository: Ignoring `.'Unable to determine path to Linux source tree.Fallin

【HTML+CSS】精灵图_weixin_30289831的博客-程序员宅基地

需求:实现下图效果(精灵图)&lt;!DOCTYPE html&gt;&lt;html&gt;&lt;head&gt; &lt;title&gt;&lt;/title&gt; &lt;style type="text/css"&gt; .huawei { display:inline-block; ...

随便推点

.NET框架源码解读之SSCLI编译过程简介_weixin_30409849的博客-程序员宅基地

前文演示了编译SSCLI最简便的方法(在Windows下):在“Visual Studio 2005 Command Prompt”下,进入SSCLI的根目录;运行 env.bat 脚本准备环境;运行 buildall.cmd 脚本开始编译过程。env.bat设置了当前SSCLI的运行环境,命令的语法是:`env [option]`,其中[option]可以是d...

java jfreechart 饼图_JFreeChart 使用一 饼图之高级特性_达哔的博客-程序员宅基地

原文链接:http://www.cnblogs.com/jtmjx/archive/2013/04/23/jfreechart_advantage.html本文主要讲解JFreeChart中饼图的一些特征。内容如下:l控制颜色和饼图片区的外廓lnull值和零值的处理l饼图片区的标签(定制文本,改变分配的比例空间)l“取出”某个片区l多个饼图显示l显示3D效果的饼图更多的信息,可以参见PiePlot...

文件数据读取【不带注释版本】_HanLongXia的博客-程序员宅基地

/* *说明:把分区0-6写到block0.txt-block6.txt中*/#include &lt;stdio.h&gt;#include &lt;sys/ioctl.h&gt;#include &lt;string.h&gt;#include &lt;stdlib.h&gt;#include &lt;stdbool.h&gt;#include &lt;sys/mount.h&gt;#include &lt;sys/types.h&gt;#include &lt;dirent.h&

DIY机械键盘过程照_HuWenjin的博客-程序员宅基地

我是个程序员,偶而搞下硬件开发,总觉得别人设计的键盘不满足自己的胃口,我自己DIY了一把机械键盘。全键盘键位可编程,可以从内核开始编程,共设计69键,有点类似HHKB,但是比她要多了几个键盘。功能设计和键位图吧,全键位可自己指定。硬件主控方案:单片机+USB接口芯片这样的目的是让会单片机开发的朋友可以自己发挥,自己写主控程序。后期如果有朋友喜欢,需要的话,可以开发专用驱动,

喝汽水问题._华为面试题 1块钱 一瓶汽水 20块钱_Lange_Taylor的博客-程序员宅基地

喝汽水,1瓶汽水1元,2个空瓶可以换一瓶汽水,给20元,可以多少汽水。编程实现。39瓶.面试情况下还可以借一瓶变为40瓶#include &lt;stdio.h&gt;#include &lt;stdlib.h&gt;int main() { int money = 20; int bottle = money; int sum = money; while (bottle ...

【弦图染色】【bzoj 1006】: [HNOI2008]神奇的国度_willinglive的博客-程序员宅基地

http://www.lydsy.com/JudgeOnline/problem.php?id=1006MCS算法见cdq论文//#define _TEST _TEST#include #include #include #include #include #include #include using namespace std;/***********

推荐文章

热门文章

相关标签