iOS - TableView的批量刪除的三種方式

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下載地址

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容