還是在上一個程序的基礎上進行修改
1、反選按鈕
2、全選按鈕
3、刪除按鈕
4、其他代碼優化
1、反選按鈕
反選的過程就是將_deleteShops數組中得數據清空,然后將Shops中數組添加到_deleteShops數組
添加一個 UIBarButtonItem 按鈕,綁定響應事件.
代碼如下
1 // 反選 2 - (void)unSelected 3 { 4 // 1、記錄shops數組的長度和_deleteShops的長度 5 NSInteger shopsCount = _shops.count; 6 NSInteger deleteCount = _deleteShops.count; 7 8 // 2、將數據全部添加到臨時數組中,有先后順序:shops在前,deleteshop在后 9 NSMutableArray *tmp = [NSMutableArray arrayWithArray:_shops]; 10 [tmp addObjectsFromArray:_deleteShops]; 11 12 // 3、添加數據到_deleteShops數組,取出前一部分 13 for (NSInteger i = 0 ; i < shopsCount ;i ++) 14 { 15 Shop *s = [tmp objectAtIndex:i]; 16 // 添加數據到_deleteShops數組 17 [_deleteShops addObject:s]; 18 19 } 20 // 4、將取消選中的按鈕從_deleteShops數組中移除數組范圍(shopsCount,)后一部分, 21 for (NSInteger i = shopsCount ; i < (shopsCount + deleteCount);i ++) 22 { 23 Shop *s = [tmp objectAtIndex:i]; 24 [_deleteShops removeObject:s]; 25 26 } 27 28 // 5、更新表格 29 [_tableView reloadData]; 30 }
2、全選\全不選按鈕
全選\全不選按鈕的實現主要在_deleteShops數組的數據進行增減
1 // 全選\全不選按鈕 2 - (void)selectAll 3 { 4 // 1、如果一樣就清空deleteShop數組 5 if(_deleteShops.count == _shops.count) 6 { 7 [_deleteShops removeAllObjects]; 8 } 9 // 2、否則就將shops數組中數據添加到deleteshops數組中 10 else 11 { 12 // 先清空deleteshop數組 13 [_deleteShops removeAllObjects]; 14 // 再添加數據 15 for (NSInteger i = 0 ; i < _shops.count ;i ++) 16 { 17 Shop *s = [_shops objectAtIndex:i]; 18 // 添加數據到_deleteShops數組 19 [_deleteShops addObject:s]; 20 21 } 22 } 23 // 3、更新表格 24 [_tableView reloadData]; 25 }
3、刪除按鈕
_deleteShops數組中保存的就是要刪除的數據,直接從_shops數組中對數據進行刪除就行
1 // 刪除選中行 2 -(void)remove 3 { 4 // 1、刪除行數據 5 [_shops removeObjectsInArray:_deleteShops]; 6 // 2、刪除_deleteShops數組 7 [_deleteShops removeAllObjects]; 8 // 3、更新表格 9 [self.tableView reloadData]; 10 }
4、其他代碼實現
在上述按鈕按下的過程中會有幾個控件的狀態改變,刪除按鈕狀態、選中行數量(lable標簽)的狀態、全選按鈕狀態、反選按鈕狀態。每次都會產生數據的更改,所以每次都需要對數據界面進行刷新。
所以把這些狀態的改變放到方法numberOfRowsInSection中
1 // 設置行 2 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 3 { 4 // 因為每次選中這兩個值都會同時改變,所以放在這里會更好,可以省去很多代碼 5 // 更新行時判斷選中cell個數顯示方式,每次改變都會調用 6 _textlable.text = (_deleteShops.count == 0) ? @"淘寶" : [NSString stringWithFormat:@"淘寶(%d)",_deleteShops.count]; 7 // 刪除按鈕狀態 8 _buttonDelete.enabled = (_deleteShops.count == 0) ? NO : YES; 9 // 反選按鈕狀態 10 _unSelectBtn.enabled = (_shops.count == 0) ? NO : YES; 11 // 全選按鈕狀態 12 _selectAllBtn.enabled = (_shops.count == 0) ? NO : YES; 13 return _shops.count; 14 15 }
效果如下:
主要代碼
1 // 2 // SLQViewController.h 3 // UITableView-淘寶 4 // 5 // Created by Christian on 15/5/18. 6 // Copyright (c) 2015年 slq. All rights reserved. 7 // 8 9 #import <UIKit/UIKit.h> 10 11 @interface SLQViewController : UIViewController 12 @property (weak, nonatomic) IBOutlet UILabel *textlable; // lable標簽 13 @property (weak, nonatomic) IBOutlet UIBarButtonItem *buttonDelete; // 刪除按鈕 14 - (IBAction)remove; // 刪除事件 15 - (IBAction)unSelected; // 反選事件 16 - (IBAction)selectAll; // 全選 17 @property (weak, nonatomic) IBOutlet UITableView *tableView; // tableView 18 @property (weak, nonatomic) IBOutlet UIBarButtonItem *unSelectBtn; // 反選按鈕 19 @property (weak, nonatomic) IBOutlet UIBarButtonItem *selectAllBtn; // 全選按鈕 20 21 @end
1 // 2 // SLQViewController.m 3 // UITableView-淘寶 4 // 5 // Created by Christian on 15/5/18. 6 // Copyright (c) 2015年 slq. All rights reserved. 7 // 8 9 #import "SLQViewController.h" 10 #import "Shop.h" 11 @interface SLQViewController () <UITableViewDataSource, UITableViewDelegate> 12 13 14 { 15 NSMutableArray *_shops; 16 NSMutableArray *_deleteShops; 17 } 18 @end 19 20 @implementation SLQViewController 21 22 - (void)viewDidLoad 23 { 24 [super viewDidLoad]; 25 // Do any additional setup after loading the view, typically from a nib. 26 27 // 讀取*.plist文件 28 // 1.獲取全路徑 29 NSString *path = [[NSBundle mainBundle] pathForResource:@"shops" ofType:@"plist"]; 30 // 2.讀取數據到數組 31 NSArray *array = [NSArray arrayWithContentsOfFile:path]; 32 // 初始化數組 33 _shops = [NSMutableArray array]; 34 _deleteShops = [NSMutableArray array]; 35 //NSLog(@"%d",array.count); 36 // 添加數據到界面 37 for (NSDictionary *arr in array) 38 { 39 // 1.創建shop 40 Shop *s = [Shop shopWithDict:arr]; 41 // 2.添加到數組 42 [_shops addObject:s]; 43 } 44 //_buttonDelete.enabled = YES; 45 46 } 47 48 // 刪除選中行 49 -(void)remove 50 { 51 // 1、刪除行數據 52 [_shops removeObjectsInArray:_deleteShops]; 53 // 2、刪除_deleteShops數組 54 [_deleteShops removeAllObjects]; 55 // 3、更新表格 56 [self.tableView reloadData]; 57 } 58 59 // 反選 60 - (void)unSelected 61 { 62 // 1、記錄shops數組的長度和_deleteShops的長度 63 NSInteger shopsCount = _shops.count; 64 NSInteger deleteCount = _deleteShops.count; 65 66 // 2、將數據全部添加到臨時數組中,有先后順序:shops在前,deleteshop在后 67 NSMutableArray *tmp = [NSMutableArray arrayWithArray:_shops]; 68 [tmp addObjectsFromArray:_deleteShops]; 69 70 // 3、添加數據到_deleteShops數組,取出前一部分 71 for (NSInteger i = 0 ; i < shopsCount ;i ++) 72 { 73 Shop *s = [tmp objectAtIndex:i]; 74 // 添加數據到_deleteShops數組 75 [_deleteShops addObject:s]; 76 77 } 78 // 4、將取消選中的按鈕從_deleteShops數組中移除數組范圍(shopsCount,)后一部分, 79 for (NSInteger i = shopsCount ; i < (shopsCount + deleteCount);i ++) 80 { 81 Shop *s = [tmp objectAtIndex:i]; 82 [_deleteShops removeObject:s]; 83 84 } 85 86 // 5、更新表格 87 [_tableView reloadData]; 88 } 89 // 全選\全不選按鈕 90 - (void)selectAll 91 { 92 // 1、如果一樣就清空deleteShop數組 93 if(_deleteShops.count == _shops.count) 94 { 95 [_deleteShops removeAllObjects]; 96 } 97 // 2、否則就將shops數組中數據添加到deleteshops數組中 98 else 99 { 100 // 先清空deleteshop數組 101 [_deleteShops removeAllObjects]; 102 // 再添加數據 103 for (NSInteger i = 0 ; i < _shops.count ;i ++) 104 { 105 Shop *s = [_shops objectAtIndex:i]; 106 // 添加數據到_deleteShops數組 107 [_deleteShops addObject:s]; 108 109 } 110 } 111 // 3、更新表格 112 [_tableView reloadData]; 113 } 114 // 設置行 115 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 116 { 117 // 因為每次選中這兩個值都會同時改變,所以放在這里會更好,可以省去很多代碼 118 // 更新行時判斷選中cell個數顯示方式,每次改變都會調用 119 _textlable.text = (_deleteShops.count == 0) ? @"淘寶" : [NSString stringWithFormat:@"淘寶(%d)",_deleteShops.count]; 120 // 刪除按鈕狀態 121 _buttonDelete.enabled = (_deleteShops.count == 0) ? NO : YES; 122 // 反選按鈕狀態 123 _unSelectBtn.enabled = (_shops.count == 0) ? NO : YES; 124 // 全選按鈕狀態 125 _selectAllBtn.enabled = (_shops.count == 0) ? NO : YES; 126 return _shops.count; 127 128 } 129 // 設置行內容 130 // 每當有一個cell進入視野范圍內就會調用 131 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 132 { 133 static NSString *ID = @"C1"; 134 // 從緩存池中選擇可循環利用的cell,指定標識c1,這樣就會找到結構一樣的cell 135 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; 136 // 如果緩存池中沒有 137 if (cell == nil) 138 { 139 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; // 設定標識C1 140 } 141 // UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"c1"]; 142 // 更新數據到界面 143 Shop *s = _shops[indexPath.row]; 144 cell.textLabel.text = s.name; 145 cell.imageView.image = [UIImage imageNamed:s.icon];; 146 cell.detailTextLabel.text = s.desc; 147 // 顯示最右側的按鈕 148 if ([_deleteShops containsObject:s]) // 判斷是否已經選中的cell,是得話設置圖標 149 { 150 cell.accessoryType = UITableViewCellAccessoryCheckmark; 151 } 152 else // 否則就什么都不顯示 153 { 154 cell.accessoryType = UITableViewCellAccessoryNone; 155 } 156 157 // NSLog(@"%p,第%ld行數據",cell,indexPath.row); 158 159 return cell; 160 } 161 // 設置每一行的高度 162 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 163 { 164 // 165 //NSLog(@"height is 70"); 166 return 100; 167 } 168 // 選中某行執行 169 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 170 { 171 //NSLog(@"selected"); 172 //選中后顏色變深 173 // 在最右側顯示一個對號圖標 174 // 1、獲得選中行 175 Shop *s = _shops[indexPath.row]; 176 // 2、修改選中行的數據,將選中的cell添加到待刪除數組中 177 if ([_deleteShops containsObject:s]) // 如果已經存在,再次點擊就取消選中按鈕 178 { 179 [_deleteShops removeObject:s]; 180 } 181 else // 否則就添加待刪除數組 182 { 183 [_deleteShops addObject:s]; 184 } 185 // 3、更新數據 186 [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 187 // 4、顯示選中條數 188 if(_deleteShops.count == 0) 189 { 190 _textlable.text = @"淘寶"; 191 _buttonDelete.enabled = NO; 192 } 193 else 194 { 195 _textlable.text = [NSString stringWithFormat:@"淘寶(%d)",_deleteShops.count]; 196 _buttonDelete.enabled = YES; 197 } 198 199 } 200 // 取消選中某行執行 201 - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath 202 { 203 NSLog(@"Deselected"); 204 } 205 206 207 @end
源代碼:http://pan.baidu.com/s/1mgIIUEk