標籤:des style blog http 使用 io strong 資料
UITableView.06:
【1】拖入ToolBar,TableView
【2】連線,設定代理模式,資料來源等(ToolBar中的垃圾桶也需要串連方法removeRow)
【3】代碼
1.聲明
mydata :所有的資料,人工初始化的資料30行。
selectedData:選中的資料,視頻中點擊後選中的資料
selectedRows:只是為了刪除時候能得到所選擇的資料行而建立的數組
@interface ViewController ()// 所有的總資料@property (strong,nonatomic) NSMutableArray *mydata;// 選中的資料@property (strong,nonatomic) NSMutableArray *selectedData;// 拿到被選中的資料內容/********* 這個是用來顯示刪除動畫而選中的資料行 ********/@property (strong,nonatomic)NSMutableArray *selectedRows;@end
2.初始化資料
- (void)viewDidLoad{ [super viewDidLoad];
// 初始化資料 self.mydata=[NSMutableArray array]; self.selectedData=[NSMutableArray array]; self.selectedRows=[NSMutableArray array];
//建立30個對象存放到數組mydata中去
for(int i=0;i<30;i++) { NSString *text=[NSString stringWithFormat:@"哈哈哈--%d",i]; [self.mydata addObject:text]; } }
3.設定返回行數
#pragma mark- 資料來源方法-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.mydata.count;}
4. 判斷選中的需要不需要打鉤
#pragma mark 每當有一個cell進入視野範圍內就會調用,返回當前這行顯示的cell- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ // 0.用static修飾的局部變數,只會初始化一次< # # > static NSString *ID = @"Cell"; // 1.拿到一個標識先去緩衝池中尋找對應的Cell UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; // 2.如果緩衝池中沒有,才需要傳入一個標識建立新的Cell if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; }
// 取出當前這行資料【判斷是否打鉤】 NSString *curentText=self.mydata[indexPath.row]; // 3.覆蓋資料 cell.textLabel.text = self.mydata[indexPath.row];
// 4.覆蓋狀態【判斷是否打鉤】 if([self.selectedData containsObject:curentText]) // 判斷選中的這行資料內容和我數組裡面的內容是否相等,如果相等 { //需要打鉤 cell.accessoryType=UITableViewCellAccessoryCheckmark; }else { cell.accessoryType=UITableViewCellAccessoryNone; } return cell;}
5.選中方法,調用,需要使用代理監聽
/******************************************** 重新整理,更新介面 *************************/ |
[tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle]; |
// 重新整理當前行 |
[tableView reloadData]; |
// 全域重新整理 |
#pragma mark- 代理方法#pragma mark 該方法選中某一行就會調用-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ // 1.通過代碼取消選中當前這行(去掉藍色背景)【點擊一下背景變藍色,閃爍一下】 [tableView deselectRowAtIndexPath:indexPath animated:YES]; // animated 是否需要動畫 // 2.取出這行對應的資料 NSString *currentText = self.mydata[indexPath.row]; // 3【新】.應用MVC修改資料控制介面內容 if([self.selectedData containsObject:currentText]) { // 刪除資料 [self.selectedData removeObject:currentText]; /****顯示刪除動畫的數組中移除這個行****/ [self.selectedRows removeObject:indexPath]; }else { // 添加資料 [self.selectedData addObject:currentText]; /****顯示刪除動畫的數組中添加這個行****/ [self.selectedRows addObject:indexPath]; } /************************ 重新整理,更新介面 *************************/ [tableView reloadRowsAtIndexPaths:@[indexPath] // 重新整理這一行 withRowAnimation:UITableViewRowAnimationMiddle]; // 重新整理當前行 // [tableView reloadData]; // 全域重新整理 }
//【與上面【3】新,同樣的操作】
/*************下面注釋掉的是直接進行介面的更改,違反了MVC模式,所以不建議使用**************/ /* // 3.讓選中的行打鉤,對應的cell打鉤 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; // 這個方法就是通過indexPath得到所 // 判斷selectedData數組中是否包含currentText,如果已經包含(說明已經打鉤) if([self.selectedData containsObject:currentText]) { // 取消打鉤 cell.accessoryType=UITableViewCellAccessoryNone; // 刪除資料 [self.selectedData removeObject:currentText]; } else { // 沒有包含這行資料,(說明沒有打鉤) // 打鉤 cell.accessoryType=UITableViewCellAccessoryCheckmark; // 添加資料 [self.selectedData addObject:currentText]; }*/
6.設定點擊垃圾桶按鈕操作
注意:
| 作用 |
代碼 |
注意事項 |
重新整理選中單行 |
[self.tableView reloadRowsAtIndexPaths:self.selectedRows withRowAnimation:UITableViewRowAnimationMiddle]; |
資料個數要保持不變,否則也要報錯 |
刪除選中單行 |
[self.tableView deleteRowsAtIndexPaths:self.selectedRows withRowAnimation:UITableViewRowAnimationLeft]; |
比如說,移除了3行,那麼對應的數組中也需要移除3行。 |
#pragma mark - 按垃圾桶刪除對應的行-(IBAction)removeRow{ // 刪除資料分兩步 // 1.更改資料 [self.mydata removeObjectsInArray:self.selectedData]; // 2.重新整理UI介面 // [self.tableView reloadData]; /*******這個方法的要求:再資料count不變。重新整理固定的那幾行********/ /*【[self.tableView reloadRowsAtIndexPaths:self.selectedRows withRowAnimation:UITableViewRowAnimationMiddle]; 】*/ /*******這個方法的要求:這個使用需要也是數組中刪除一定的資料(這個方法裡挪掉多少個,資料也要挪掉多少個)********/ [self.tableView deleteRowsAtIndexPaths:self.selectedRows withRowAnimation:UITableViewRowAnimationLeft]; // 3.清除選中的所有資料 [self.selectedData removeAllObjects]; [self.selectedRows removeAllObjects];}
其他:
遵循協議後【取消選中將監聽的方法】
#pragma mark 取消選中-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"取消選中了%d",indexPath.row);}