標籤:
採用存取indexPath的方式,來對多個選中的儲存格進行刪除
刪除前:
刪除後:
分析:如何?刪除多個儲存格呢?這需要用到UITableView的代理方法,即選中儲存格時對儲存格做的處理,同時我們也要定義一個可變的數組,用來儲存選中的資料,以便後來的刪除。這裡採用儲存indexPath的方式,因為每選中一個儲存格時,它都對應著一個indexPath,同時,將選中的儲存格添加指引視圖,即打對勾,也要判斷打對勾是否重複,根據此來顯示儲存格的標記。最後,將標記的資料全部在原資料庫中刪除,同時在清空儲存資料的數組,重新整理表格即可。刪除過程中,涉及到排序問題,下面的代碼中將有介紹。
1 #import "ViewController.h" 2 #define NUM 20 3 @interface ViewController ()<UITableViewDataSource,UITableViewDelegate> 4 @property (strong,nonatomic)NSMutableArray *products; //原始的產品庫存 5 @property (strong,nonatomic)NSMutableArray *cellIndexPaths; //存放選中的儲存格 6 @property (weak, nonatomic) IBOutlet UITableView *tableView; 7 - (IBAction)deleteButtonClicked:(UIBarButtonItem *)sender; 8 9 @end 10 11 @implementation ViewController 12 13 - (void)viewDidLoad { 14 [super viewDidLoad]; 15 //初始化 16 self.products = [NSMutableArray arrayWithCapacity:NUM]; 17 self.cellIndexPaths = [NSMutableArray arrayWithCapacity:NUM]; 18 for(int i=0; i<NUM; i++) 19 { 20 NSString *product = [NSString stringWithFormat:@"product-%02d",i]; 21 [self.products addObject:product]; 22 } 23 24 //設定資料來源和代理 25 self.tableView.dataSource = self; 26 self.tableView.delegate = self; 27 } 28 29 //刪除所有選中的儲存格的IndexPath 30 //說明:在每一次進行刪除的時候,如果總是從數組的後面刪除,結果是沒有影響的,但是,如果從前面開始刪除的話,那麼數組中後面的元素會依次向前遞進,此時它們的indexPath就全改變了,結果就會出問題。此時,就需要對選中的元素進行排序操作。 31 - (IBAction)deleteButtonClicked:(UIBarButtonItem *)sender 32 { 33 //對選中的元素進行排序操作(按升序排列) 34 [self.cellIndexPaths sortUsingComparator:^NSComparisonResult(id obj1, id obj2) { 35 NSIndexPath *ip1 = (NSIndexPath*)obj1; 36 NSIndexPath *ip2 = (NSIndexPath*)obj2; 37 if(ip1.row == ip2.row) 38 { 39 return NSOrderedSame; 40 } 41 else if(ip1.row > ip2.row) 42 { 43 return NSOrderedDescending; 44 } 45 else 46 { 47 return NSOrderedAscending; 48 } 49 50 }]; 51 52 //1.從未經處理資料中刪除選中的儲存格中的產品(倒著刪除) 53 [self.cellIndexPaths enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 54 [self.products removeObjectAtIndex:((NSIndexPath*)obj).row]; 55 }]; 56 57 //2.清空記錄選中的數組 58 NSArray *tempArray = [NSArray arrayWithArray:self.cellIndexPaths]; 59 [self.cellIndexPaths removeAllObjects]; 60 61 //3.進行局部的重新整理 62 [self.tableView deleteRowsAtIndexPaths:tempArray withRowAnimation:UITableViewRowAnimationLeft]; 63 } 64 65 #pragma mark -tableView的資料來源方法 66 //每一個section有多少個row 67 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 68 { 69 return self.products.count; 70 } 71 //設定每一個儲存格的內容 72 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 73 { 74 //1.根據reuseIdentifier,先到對象池中去找重用的儲存格對象 75 static NSString *reuseIdentifier = @"productCell"; 76 UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; 77 //2.如果沒有找到,自己建立儲存格對象 78 if(cell == nil) 79 { 80 cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier]; 81 } 82 //3.設定儲存格對象的內容 83 cell.textLabel.text = self.products[indexPath.row]; 84 85 if([self.cellIndexPaths containsObject:indexPath]) //初次選中時,標記一下 86 { 87 cell.accessoryType = UITableViewCellAccessoryCheckmark; 88 } 89 else //再次選中時,取消標幟 90 { 91 cell.accessoryType = UITableViewCellAccessoryNone; 92 } 93 94 return cell; 95 } 96 97 #pragma mark -tableView的代理方法 98 //對選中的儲存格的處理 99 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath100 {101 //1.取出目前的儲存格102 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];103 104 //3.將取出的儲存格所在的indexPath添加到數組中,並給儲存格添加輔助指引視圖105 if([self.cellIndexPaths containsObject:indexPath]) //已經存在106 {107 cell.accessoryType = UITableViewCellAccessoryNone;108 109 //從數組中刪除110 [self.cellIndexPaths removeObject:indexPath];111 }112 else113 {114 cell.accessoryType = UITableViewCellAccessoryCheckmark;115 116 //添加到數組中117 [self.cellIndexPaths addObject:indexPath];118 }119 }120 121 @end
iOS:多個儲存格的刪除(方法一)