
01批量刪除.gif
在以往的慣性思維中,對(duì)于批量刪除的操作一直以為是標(biāo)記被選中的Cell 控件進(jìn)行刪除,但往往前面被選中的Cell會(huì)被后面顯示的Cell復(fù)用,導(dǎo)致后面顯示的Cell也是被勾選中的狀態(tài),如果我們對(duì)被選中的數(shù)據(jù)模型進(jìn)行標(biāo)記刪除,則不會(huì)出現(xiàn)上述cell被復(fù)用的情況。
第一種方式:
第一步: 在數(shù)據(jù)模型中添加一個(gè)標(biāo)記的屬性
#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;
/** 狀態(tài)量標(biāo)識(shí)有無(wú)被打鉤 */
@property (assign, nonatomic, getter=isChecked) BOOL checked;
+ (instancetype)dealWithDict:(NSDictionary *)dict;
@end
第二步:在cell設(shè)置屬性中,設(shè)置打勾的視圖是否顯示,在Xib中默認(rèn)是隱藏狀態(tài)。
- (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:@"%@人已購(gòu)買(mǎi)", sourceModel.buyCount];
// 設(shè)置打鉤控件的顯示和隱藏
self.checkView.hidden = !sourceModel.isChecked;
}
第三步:遍歷整個(gè)模型數(shù)組中被選中的數(shù)據(jù),并將被選中的數(shù)據(jù)添加到一個(gè)臨時(shí)的數(shù)組中tempArray ,并刪除模型數(shù)據(jù)中的被選中的數(shù)組模型。
// 批量刪除數(shù)據(jù)
- (IBAction)removeSources:(id)sender {
//建立一個(gè)臨時(shí)數(shù)組來(lái)存儲(chǔ)需要被選中的刪除數(shù)據(jù)
NSMutableArray * tempArray = [NSMutableArray array];
for (SourceModel *sources in self.sourceArray) {
if (sources.checked) {
[tempArray addObject:sources];
}
}
// 刪除模型數(shù)組里的被選中的數(shù)組
[self.sourceArray removeObjectsInArray:tempArray];
[self.tableView reloadData];
}
第二種方式:
第一步:添加一個(gè)將要被刪除的模型數(shù)組@property (nonatomic,strong)NSMutableArray *deleteArray;,并且判斷deleteArray中是否包含了被選中的模型數(shù)據(jù)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// 取消選中某一行
[tableView deselectRowAtIndexPath:indexPath animated:YES];
SourceModel *sourceModel = self.sourceArray[indexPath.row];
if ([self.deleteArray containsObject:sourceModel]) {//數(shù)組中包含了被選中的
[self.deleteArray removeObject:sourceModel];
}else {
[self.deleteArray addObject:sourceModel];
}
[tableView reloadData];
}
第二步:將sourceArray的數(shù)組模型里刪除deleteArray,并將deleteArray清空,否則deleteArray的數(shù)組一直增加元素。
// 批量刪除數(shù)據(jù)
- (IBAction)removeSources:(id)sender {
// 從數(shù)據(jù)數(shù)組中刪除被選中的模型數(shù)組
[self.sourceArray removeObjectsInArray:self.deleteArray];
[self.tableView reloadData];
//清空將要?jiǎng)h除的數(shù)組模型,
[self.deleteArray removeAllObjects];
}
第三種方式:這種方式是蘋(píng)果自帶的批量刪除操作,依據(jù)被選中的行號(hào)進(jìn)行刪除 ,其可定制性比較差。

03批量刪除.gif
第一步:允許
TableView進(jìn)行多選操作。
- (void)viewDidLoad {
[super viewDidLoad];
// 允許在編輯模式下進(jìn)行多選操作
self.tableView.allowsMultipleSelectionDuringEditing = YES;
第二步:批量選中操作。
//批量選中
- (IBAction)multiOperation:(id)sender {
[self.tableView setEditing:!self.tableView.isEditing animated:YES];
}
第三步:將選中的數(shù)據(jù)進(jìn)行刪除。
//刪除數(shù)據(jù)
- (IBAction)removeSources:(id)sender {
// 獲取被選中的行號(hào)
NSArray *indexPaths = [self.tableView indexPathsForSelectedRows];
//便利所有的行號(hào)
NSMutableArray *deleteArray = [NSMutableArray array];
for (NSIndexPath *paths in indexPaths) {
[deleteArray addObject:self.sourceArray[paths.row]];
}
// 刪除被選中的行號(hào)模型數(shù)據(jù)
[self.sourceArray removeObjectsInArray:deleteArray];
[self.tableView reloadData];
}