iOS:多个单元格的删除(方法二):
生活随笔
收集整理的這篇文章主要介紹了
iOS:多个单元格的删除(方法二):
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前面介紹了萬無一失的方法一,這里介紹刪除單元格的第二種方式,通過刪除單元格中的內容的方式進行操作:(但是這種情況有一個小的弊端,由于單元格重用機制,如果單元格內容一樣時,標記的存在會造成誤刪)
刪除前:
?
刪除后:
分析如下:(如果每一個單元格內容都不一樣)采取刪除單元格內容的方式是比較簡單的方式,那么如何實現多個單元格的刪除呢?
首先,定義兩個必要的可變的數組,一個是用來存儲初始化原始數據的,另一個是用來存儲選中單元格后,從里面取出來的數據;
其次,通過數據源的方法將原始數據顯示在表格中,同時通過代理的方法,即選中單元格的處理,來給選中的單元格添加指引視圖(標記),并將首先選中的單元格內容取出存到數組中,(二次選中則將其取消標記并從數組中刪除);
最后,原始數據數組將所有選中的單元格內容全部刪除,與此同時,數據選中存儲數組也直接清空數組,然后,將表格進行整體刷新即可。
?
代碼如下:
1 #import "ViewController.h"2 #define NUM 203 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 @end11 12 @implementation ViewController13 - (IBAction)deleteButtonClicked:(UIBarButtonItem *)sender14 {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有多少個row44 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section45 {46 return self.products.count;47 }48 //設置每一個單元格的內容49 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath50 {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 *)indexPath84 {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?
轉載于:https://www.cnblogs.com/daxiong520/p/4915994.html
總結
以上是生活随笔為你收集整理的iOS:多个单元格的删除(方法二):的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: npm WARN install Ref
- 下一篇: [原创] SQLite数据库使用清单(上