標籤:
1. UITableView編輯 1> UITableView 編輯流程
2> UITableView 編輯步驟(四步)
① 第一步 : 讓 TableView 處於編輯狀態(在按鈕點擊事件方法中)
1 // 最佳化寫法2 // 不帶動畫3 _rootView.tableView.editing = !_rootView.tableView.editing;4 // 帶動畫5 [_rootView.tableView setEditing:!_rootView.tableView.editing animated:YES];
② 協議設定
第二步 : 確定cell是否處於編輯狀態(UITableViewDataSource協議的方法)
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{ // 所有的cell都可以進行編輯時,整個方法可以省略 // return YES; // 只有第一個分區可以被編輯 if (0 == indexPath.section) { return YES; } return NO;}
第三步 : 設定cell的編輯樣式 (刪除 , 添加)
1 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath2 {3 return UITableViewCellEditingStyle枚舉中的樣式;4 }
第四步 : 編輯狀態進行添加
1 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath2 {3 // 1. 處理資料4 // 2. 更新UI介面5 } 3> 添加(前兩步通用)
第三步:
1 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath2 {3 return UITableViewCellEditingStyleDelete;4 }
第四步:
1 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 2 { 3 // 1. 刪除資料 4 [_allDataArray[indexPath.section] removeObjectAtIndex:indexPath.row]; 5 6 // 2. 更新UI 7 // 單獨更新一行(刪除) 8 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight]; 9 10 // 全部更新11 // [tableView reloadData];12 } 4> 刪除(前兩步通用)
第三步:
1 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath2 {3 return UITableViewCellEditingStyleInsert;4 }
第四步:
1 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 2 { 3 // 1. 插入資料到數組中 4 [_allDataArray[indexPath.section] insertObject:@"你是不是傻" atIndex:indexPath.row + 1]; 5 6 // 2. 更新UI 7 // 全部更新 8 // [tableView reloadData]; 9 10 // 單獨更新一行11 12 // 建立新一行的NSIndexPath對象13 NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];14 15 [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationLeft];16 } 5>添加 和 刪除 結合
第三步 : 設定一個 UITableViewCellEditingStyle 類型的 屬性(style) 用於儲存 添加 和 刪除 的編輯的樣式, 在 添加 和 刪除 對應的點擊事件方法中賦值, 注:按鈕的功能樣式需要在點擊事件最上面實現, 否則會出現bug
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ return self.style;}
第四步 :
1 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath2 {3 // 判斷編輯樣式4 if (UITableViewCellEditingStyleDelete == editingStyle) {5 刪除操作,詳情請見 3> 第四步6 } else if (UITableViewCellEditingStyleInsert == editingStyle){7 添加操作,詳情請見 4> 第四步8 }9 } 6> 移動
① (在 TableView 處於編輯狀態下)實現協議: 告訴 tableView 是否能夠移動
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{ return YES;}
② 移動
1 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath 2 { 3 // 1. 擷取需要修改的資料 4 NSString *sourceData = [self.allDataArray[sourceIndexPath.section] objectAtIndex:sourceIndexPath.row]; 5 6 // 2. 先將資料從當前的位置移除 7 [self.allDataArray[sourceIndexPath.section] removeObjectAtIndex:sourceIndexPath.row]; 8 9 // 3. 將資料插入到對應的位置10 [self.allDataArray[destinationIndexPath.section] insertObject:sourceData atIndex:destinationIndexPath.row];11 }
bug修正--- 防止跨分區移動
1 - (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath 2 { 3 // sourceIndexPath 為原位置 4 // proposedDestinationIndexPath 為將要移動到的位置 5 if (sourceIndexPath.section == proposedDestinationIndexPath.section) { 6 return proposedDestinationIndexPath; 7 } else { 8 return sourceIndexPath; 9 }10 }2. UITableViewController
1> 概述
UITableViewController 是繼承於 UIViewController 中的一個類,只不過比UIViewController 中多了一個屬性 tableView 。 即: UITableViewController 是一個內建 table 的視圖控制器。
2> 注意事項
- UITableViewController 繼承 UIViewController , 內建一個tableView
- self.view 不是 UIView 是 UITableView
- datasource 和 delegate 預設都是 self (UITableViewController)
- 開發中只需要建 UITableViewController 子類
iOSDay30之UITableView編輯