zoukankan      html  css  js  c++  java
  • iOS:多个单元格的删除(方法二):

      前面介绍了万无一失的方法一,这里介绍删除单元格的第二种方式,通过删除单元格中的内容的方式进行操作:(但是这种情况有一个小的弊端,由于单元格重用机制,如果单元格内容一样时,标记的存在会造成误删)

    删除前

    删除后:

      分析如下:(如果每一个单元格内容都不一样)采取删除单元格内容的方式是比较简单的方式,那么如何实现多个单元格的删除呢?

    首先,定义两个必要的可变的数组,一个是用来存储初始化原始数据的,另一个是用来存储选中单元格后,从里面取出来的数据;

    其次,通过数据源的方法将原始数据显示在表格中,同时通过代理的方法,即选中单元格的处理,来给选中的单元格添加指引视图(标记),并将首先选中的单元格内容取出存到数组中,(二次选中则将其取消标记并从数组中删除);

    最后,原始数据数组将所有选中的单元格内容全部删除,与此同时,数据选中存储数组也直接清空数组,然后,将表格进行整体刷新即可。

    代码如下:

      1 #import "ViewController.h"
      2 #define NUM 20
      3 
      4 @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
      5 @property (weak, nonatomic) IBOutlet UITableView *tableView;
      6 @property (strong,nonatomic)NSMutableArray *products;      //原始的数据库存
      7 @property (strong,nonatomic)NSMutableArray *productStore; //选中的数据库存
      8 - (IBAction)deleteButtonClicked:(UIBarButtonItem *)sender;
      9 
     10 @end
     11 
     12 @implementation ViewController
     13 - (IBAction)deleteButtonClicked:(UIBarButtonItem *)sender
     14 {
     15     //1.将选中的所有产品从原始库存中删除
     16     [self.productStore enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
     17         [self.products removeObject:obj];
     18     }];
     19     
     20     //2.清空选中的数据库存
     21     [self.productStore removeAllObjects];
     22     
     23     //3.整体刷新表格
     24     [self.tableView reloadData];
     25 }
     26 - (void)viewDidLoad {
     27     [super viewDidLoad];
     28     //初始化
     29     self.products = [NSMutableArray arrayWithCapacity:NUM];
     30     self.productStore = [NSMutableArray arrayWithCapacity:NUM];
     31     for(int i=0; i<NUM; i++)
     32     {
     33         NSString *product = [NSString stringWithFormat:@"product-%02d",i];
     34         [self.products addObject:product];
     35     }
     36     
     37     //设置数据源和代理
     38     self.tableView.dataSource = self;
     39     self.tableView.delegate = self;
     40 }
     41 
     42 #pragma mark -tableView的数据源方法
     43 //每一个scetion有多少个row
     44 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
     45 {
     46     return self.products.count;
     47 }
     48 //设置每一个单元格的内容
     49 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
     50 {
     51     //1.根据reuseIdentifier,先到对象池中去找重用的单元格对象
     52     static NSString *reuseIdentifier = @"productCell";
     53     UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
     54     //2.如果没有找到,自己创建单元格对象
     55     if(cell == nil)
     56     {
     57         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
     58     }
     59     //3.设置单元格对象的内容
     60     cell.textLabel.text = self.products[indexPath.row];
     61     //设置字体颜色
     62     cell.textLabel.textColor = [UIColor redColor];
     63     //设置字体大小
     64     cell.textLabel.font = [UIFont systemFontOfSize:20];
     65     //设置单元格颜色
     66     cell.tintColor  = [UIColor orangeColor];
     67     
     68     if([self.productStore containsObject:self.products[indexPath.row]]) //首次选中
     69     {
     70         //添加标记显示
     71         cell.accessoryType = UITableViewCellAccessoryCheckmark;
     72     }
     73     else //二次选中
     74     {
     75         //取消标记显示
     76         cell.accessoryType = UITableViewCellAccessoryNone;
     77     }
     78     return cell;
     79 }
     80 
     81 #pragma mark -tableView的代理方法
     82 //选中单元格时的处理
     83 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
     84 {
     85     //获取当前选中的单元格
     86     UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
     87     
     88     //取出单元格中的产品
     89     NSString *product = self.products[indexPath.row];
     90     
     91     //对选中的单元格添加辅助指引视图,并将产品存储到数组中
     92     if([self.productStore containsObject:product]) //已经选中过一次
     93     {
     94         //取消标记
     95         cell.accessoryType = UITableViewCellAccessoryNone;
     96         
     97         //将产品从存储数组中删除
     98         [self.productStore removeObject:product];
     99     }
    100     else    //首先选中
    101     {
    102         //添加标记
    103         cell.accessoryType = UITableViewCellAccessoryCheckmark;
    104         
    105         //将产品添加到存储数组中
    106         [self.productStore addObject:product];
    107     }
    108 }
    109 @end
  • 相关阅读:
    SQL中部分语法整理
    iOS获取设备唯一标识的8种方法
    动力节点Java培训告诉你Java线程的多功能用法
    Java编程中的美好
    Node.js process 模块常用属性和方法
    Android系统学习小记
    动力节点Java培训告诉你Java线程的多功能用法
    mybatis-java1234一
    一:luecne初体验
    Linux系统安装-系统分区
  • 原文地址:https://www.cnblogs.com/XYQ-208910/p/4792813.html
Copyright © 2011-2022 走看看