美文网首页iOS开发技能集锦
iOS - TableView的批量删除的三种方式

iOS - TableView的批量删除的三种方式

作者: 黄晓坚 | 来源:发表于2017-09-01 23:47 被阅读854次
01批量删除.gif

在以往的惯性思维中,对于批量删除的操作一直以为是标记被选中的Cell 控件进行删除,但往往前面被选中的Cell会被后面显示的Cell复用,导致后面显示的Cell也是被勾选中的状态,如果我们对被选中的数据模型进行标记删除,则不会出现上述cell被复用的情况。

第一种方式:

第一步: 在数据模型中添加一个标记的属性


#import <Foundation/Foundation.h>

@interface SourceModel : NSObject

@property (strong, nonatomic) NSString *buyCount;
@property (strong, nonatomic) NSString *price;
@property (strong, nonatomic) NSString *title;
@property (strong, nonatomic) NSString *icon;

/** 状态量标识有无被打钩 */
@property (assign, nonatomic, getter=isChecked) BOOL checked;

+ (instancetype)dealWithDict:(NSDictionary *)dict;


@end

第二步:在cell设置属性中,设置打勾的视图是否显示,在Xib中默认是隐藏状态。

- (void)setSourceModel:(SourceModel *)sourceModel{
    
    _sourceModel = sourceModel;
    
    self.iconView.image = [UIImage imageNamed:sourceModel.icon];
    self.pricesLabel.text = [NSString stringWithFormat:@"¥%@", sourceModel.price];
    self.titleLabel.text = sourceModel.title;
    self.buycountLabel.text = [NSString stringWithFormat:@"%@人已购买", sourceModel.buyCount];

    // 设置打钩控件的显示和隐藏
    self.checkView.hidden = !sourceModel.isChecked;
}

第三步:遍历整个模型数组中被选中的数据,并将被选中的数据添加到一个临时的数组中tempArray ,并删除模型数据中的被选中的数组模型。

// 批量删除数据
- (IBAction)removeSources:(id)sender {
    
    //建立一个临时数组来存储需要被选中的删除数据
    NSMutableArray * tempArray = [NSMutableArray array];
    
    for (SourceModel *sources in self.sourceArray) {
        
        if (sources.checked) {
        
            [tempArray addObject:sources];
            
        }
    }
    
    // 删除模型数组里的被选中的数组
    [self.sourceArray removeObjectsInArray:tempArray];
    
    [self.tableView reloadData];
    
}

第二种方式:

第一步:添加一个将要被删除的模型数组@property (nonatomic,strong)NSMutableArray *deleteArray;,并且判断deleteArray中是否包含了被选中的模型数据

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    // 取消选中某一行
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    SourceModel *sourceModel = self.sourceArray[indexPath.row];
    
    if ([self.deleteArray containsObject:sourceModel]) {//数组中包含了被选中的
       
        
        [self.deleteArray removeObject:sourceModel];
    }else {
        
        [self.deleteArray addObject:sourceModel];
    }
    
    
    [tableView reloadData];
    
}

第二步:将sourceArray的数组模型里删除deleteArray,并将deleteArray清空,否则deleteArray的数组一直增加元素。

// 批量删除数据
- (IBAction)removeSources:(id)sender {
    
    // 从数据数组中删除被选中的模型数组
    [self.sourceArray removeObjectsInArray:self.deleteArray];
    
    [self.tableView reloadData];
    
    //清空将要删除的数组模型,
    [self.deleteArray removeAllObjects];
    
}

第三种方式:这种方式是苹果自带的批量删除操作,依据被选中的行号进行删除 ,其可定制性比较差。
03批量删除.gif
第一步:允许TableView进行多选操作。
- (void)viewDidLoad {
    [super viewDidLoad];

    // 允许在编辑模式下进行多选操作
    self.tableView.allowsMultipleSelectionDuringEditing = YES;
    

第二步:批量选中操作。

//批量选中
- (IBAction)multiOperation:(id)sender {
    
    [self.tableView setEditing:!self.tableView.isEditing animated:YES];
}

第三步:将选中的数据进行删除。

//删除数据
- (IBAction)removeSources:(id)sender {
  
    // 获取被选中的行号
    NSArray *indexPaths = [self.tableView indexPathsForSelectedRows];
    
   //便利所有的行号
    NSMutableArray *deleteArray = [NSMutableArray array];
    
    for (NSIndexPath *paths in indexPaths) {
        [deleteArray addObject:self.sourceArray[paths.row]];
    }

    // 删除被选中的行号模型数据
    [self.sourceArray removeObjectsInArray:deleteArray];
    
    [self.tableView reloadData];
    
}

第一种方式:Demo下载地址
第二种方式:Demo下载地址
第三种方式:Demo下载地址

相关文章

网友评论

    本文标题:iOS - TableView的批量删除的三种方式

    本文链接:https://www.haomeiwen.com/subject/xwqljxtx.html