采用存取indexPath的方式,来对多个选中的单元格进行删除

删除前:                      

删除后:

  分析:如何实现删除多个单元格呢?这需要用到UITableView的代理方法,即选中单元格时对单元格做的处理,同时我们也要定义一个可变的数组,用来存储选中的数据,以便后来的删除。这里采用存储indexPath的方式,因为每选中一个单元格时,它都对应着一个indexPath,同时,将选中的单元格添加指引视图,即打对勾,也要判断打对勾是否重复,根据此来显示单元格的标记。最后,将标记的数据全部在原数据库中删除,同时在清空存储数据的数组,刷新表格即可。删除过程中,涉及到排序问题,下面的代码中将有介绍。

 #import "ViewController.h"
#define NUM 20
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (strong,nonatomic)NSMutableArray *products; //原始的产品库存
@property (strong,nonatomic)NSMutableArray *cellIndexPaths; //存放选中的单元格
@property (weak, nonatomic) IBOutlet UITableView *tableView;
- (IBAction)deleteButtonClicked:(UIBarButtonItem *)sender; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
//初始化
self.products = [NSMutableArray arrayWithCapacity:NUM];
self.cellIndexPaths = [NSMutableArray arrayWithCapacity:NUM];
for(int i=; i<NUM; i++)
{
NSString *product = [NSString stringWithFormat:@"product-%02d",i];
[self.products addObject:product];
} //设置数据源和代理
self.tableView.dataSource = self;
self.tableView.delegate = self;
} //删除所有选中的单元格的IndexPath
//说明:在每一次进行删除的时候,如果总是从数组的后面删除,结果是没有影响的,但是,如果从前面开始删除的话,那么数组中后面的元素会依次向前递进,此时它们的indexPath就全改变了,结果就会出问题。此时,就需要对选中的元素进行排序操作。
- (IBAction)deleteButtonClicked:(UIBarButtonItem *)sender
{
//对选中的元素进行排序操作(按升序排列)
[self.cellIndexPaths sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSIndexPath *ip1 = (NSIndexPath*)obj1;
NSIndexPath *ip2 = (NSIndexPath*)obj2;
if(ip1.row == ip2.row)
{
return NSOrderedSame;
}
else if(ip1.row > ip2.row)
{
return NSOrderedDescending;
}
else
{
return NSOrderedAscending;
} }]; //1.从原始数据中删除选中的单元格中的产品(倒着删除)
[self.cellIndexPaths enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[self.products removeObjectAtIndex:((NSIndexPath*)obj).row];
}]; //2.清空记录选中的数组
NSArray *tempArray = [NSArray arrayWithArray:self.cellIndexPaths];
[self.cellIndexPaths removeAllObjects]; //3.进行局部的刷新
[self.tableView deleteRowsAtIndexPaths:tempArray withRowAnimation:UITableViewRowAnimationLeft];
} #pragma mark -tableView的数据源方法
//每一个section有多少个row
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.products.count;
}
//设置每一个单元格的内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//1.根据reuseIdentifier,先到对象池中去找重用的单元格对象
static NSString *reuseIdentifier = @"productCell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
//2.如果没有找到,自己创建单元格对象
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
}
//3.设置单元格对象的内容
cell.textLabel.text = self.products[indexPath.row]; if([self.cellIndexPaths containsObject:indexPath]) //初次选中时,标记一下
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else //再次选中时,取消标记
{
cell.accessoryType = UITableViewCellAccessoryNone;
} return cell;
} #pragma mark -tableView的代理方法
//对选中的单元格的处理
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//1.取出当前单元格
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; //3.将取出的单元格所在的indexPath添加到数组中,并给单元格添加辅助指引视图
if([self.cellIndexPaths containsObject:indexPath]) //已经存在
{
cell.accessoryType = UITableViewCellAccessoryNone; //从数组中删除
[self.cellIndexPaths removeObject:indexPath];
}
else
{
cell.accessoryType = UITableViewCellAccessoryCheckmark; //添加到数组中
[self.cellIndexPaths addObject:indexPath];
}
} @end

最新文章

  1. 10分钟了解设计模式(C#)
  2. PHP系统声明式事务处理
  3. 全面的Seo面试题
  4. excel日期格式转换为文本格式
  5. 基于单决策树的AdaBoost
  6. Verilog学习笔记基本语法篇(十)&#183;&#183;&#183;&#183;&#183;&#183;&#183;&#183; 常用系统函数
  7. Android开源框架:AndroidAnnotations
  8. C语言之广度优先算法
  9. SRM 601(1-250pt,500pt)
  10. Html5 学习之 Html5功能判断插件 Modernizr
  11. bzoj4554: [Tjoi2016&amp;Heoi2016]游戏 二分图匹配
  12. Linux学习之路(三)Shell脚本初探
  13. python 可调用对象之类实例
  14. GD32 ------ 使用外部中断,中断函数需要延时才能读到真正电平
  15. 给定两个字符串 s 和 t,它们只包含小写字母。 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。 请找出在 t 中被添加的字母。
  16. win7 装docker
  17. 理解使用static import 机制
  18. equals和==的区别小结
  19. NSThread(II)
  20. “SecureCRT遇到一个致命的错误且必须关闭”处理办法

热门文章

  1. Python 爬虫个人记录(一)豆瓣电影250
  2. caffe 中 plot accuracy和loss, 并画出网络结构图
  3. h5行情k线开发
  4. JavaScript 网页脚本语言 由浅入深 (随笔)
  5. 【基础知识】JavaScript基础
  6. C#拖拽操作
  7. hdu 5301 Buildings (2015多校第二场第2题) 简单模拟
  8. GIT(1)----更新代码和上传代码操作的步骤
  9. spring---aop(10)---Spring AOP中AspectJ
  10. PCI DSS合规建设ASV扫描介绍