在UITableView中刪除或添加行ui
- (void)tableView:commitEditingStyle:forRowAtIndexPath:spa
- (UITableViewCellEditingStyle)tableView: editingStyleForRowAtIndexPath:代理
1.1 加載:code
UITableViewCell 的加載須要遵照UITableViewDataSource數據源協議中的三個方法:blog
@protocol UITableViewDataSource<NSObject> @required - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; // Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier: // Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls) - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; @optional - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; // Default is 1 if not implemented
1.2 滑動刪除:rem
只實現系統自帶的滑動刪除功能 只須要實現數據源下面一個方法:get
// After a row has the minus or plus button invoked (based on the UITableViewCellEditingStyle for the cell), the dataSource must commit the change // Not called for edit actions using UITableViewRowAction - the action's handler will be invoked instead - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
/** * 若是實現了這個方法,就自動實現了滑動刪除的功能 * 點擊了刪除按鈕就會調用 * 提交了一個編輯操做就會調用(操做:刪除\添加) * @param editingStyle 編輯的行爲 * @param indexPath 操做的行號 */ - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ if(editingStyle == UITableViewCellEditingStyleDelete){ [self.contactList removeObjectAtIndex:indexPath.row]; [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop]; // 更新數據至文件 BOOL isSuccess = [NSKeyedArchiver archiveRootObject:self.contactList toFile:self.docPath]; NSLog(@"%@",isSuccess?@"保存成功":@"保存失敗"); }else if (editingStyle == UITableViewCellEditingStyleInsert){ // 添加行 通常不多使用這種方式 HJContact *contact = [[HJContact alloc] init]; contact.name = @"電信"; contact.tel = @"10000"; [self.contactList insertObject:contact atIndex:indexPath.row+1]; // 刷新表格 NSIndexPath *indexPathInsert = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:0]; [self.tableView insertRowsAtIndexPaths:@[indexPathInsert] withRowAnimation:UITableViewRowAnimationTop]; } }
這樣就能夠實現cell滑動刪除,默認刪除按鈕是英文,點擊項目名->project->Localizations->添加中文支持it
1.3 編輯模式下的刪除、添加io
若是不滑動,經過點擊系統自帶的添加和刪除按鈕進行添加、刪除操做 那麼得讓表格的可編輯屬性爲YES才行,好比:點擊一個按鈕後讓表格進入編輯模式:table
#warning 導航欄刪除按鈕方法 - (void)cellDelete{ NSLog(@"%s",__func__); // self.tableView.editing = !self.tableView.editing; [self.tableView setEditing:!self.tableView.editing animated:YES]; }
此時cell會進行編輯模式,接着會調用下面方法
#pragma mark tableView代理方法 /** * 當tableView進入編輯狀態的時候會調用,詢問每一行進行怎樣的操做(添加\刪除) */ - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return indexPath.row %2 ? UITableViewCellEditingStyleDelete : UITableViewCellEditingStyleInsert; }
注意:這個兩個方法,一個只是讓表格可編輯,接着調用
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
方法,告訴tableView那些行是刪除或添加,而後再接着調用
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
真正的添加、刪除操做都在此方法中進行.
#pragma mark 表格控件提交編輯模式- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"編輯樣式 %d", editingStyle); // 判斷表格是否須要刪除數據 if (editingStyle == UITableViewCellEditingStyleDelete) { // 1. 刪除_dataList中的數據 [_dataList removeObjectAtIndex:indexPath.row]; NSLog(@"%@", _dataList); // 2. 更新表格顯示 [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight]; } else { // 1. 在_dataList中插入數據 // 能夠在傳入參數indexPath.row + 1 的位置插入數據 // 也就是用戶當前選中行的下一行 [_dataList insertObject:@"新增數據" atIndex:(indexPath.row + 1) ]; NSLog(@"%@", _dataList); // 2. 更新表格顯示 // 界面上添加數據的newPath.row應該是參數indexPath.row + 1 NSIndexPath *newPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:0]; [_tableView insertRowsAtIndexPaths:@[newPath] withRowAnimation:UITableViewRowAnimationTop]; }}#pragma mark 返回表格的編輯樣式:刪除、新增 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { // NSLog(@"告訴表格編輯樣式"); return _tableView.tag; } #pragma mark - Actions #pragma mark 刪除操做 - (IBAction)remove:(id)sender { [_tableView setTag:UITableViewCellEditingStyleDelete]; // 再次點擊刪除按鈕,要恢復到正常瀏覽模式 // 編輯|瀏覽 // 根據表格的isEditing屬性能夠判斷出當前是否處於編輯狀態 // 若是是編輯狀態,則改成瀏覽狀態,不然,改成編輯狀態 BOOL isEditing = _tableView.isEditing; // 1. 開啓表格的編輯模式 [_tableView setEditing:!isEditing animated:YES]; } #pragma mark 添加操做 - (IBAction)add:(id)sender { /** 新增或者刪除操做方法的執行順序 1. 由按鈕開啓編輯樣式 setEditing 2. 表格會對屏幕上每個行調用 tableView:editingStyleForRowAtIndexPath: 告訴當前的行是什麼編輯樣式(新增或是刪除) 注意:若是不實現這一方法,默認都是刪除樣式 3. 當用戶點擊屏幕上相關按鈕:加好或者減號,會調用: tableView:commitEditingStyle:forRowAtIndexPath: 方法,控制器須要在此方法中更新數據,同時更新界面 */ // 利用表格的tag屬性記錄當前的編輯樣式是新增 [_tableView setTag:UITableViewCellEditingStyleInsert]; // 判斷表格當前是不是編輯模式,若是是,進入瀏覽狀態,若是不是,進入編輯狀態 BOOL isEditing = _tableView.isEditing; NSLog(@"開啓編輯模式"); // 開啓表格的編輯模式 [_tableView setEditing:!isEditing animated:YES]; }