ios UITableView左滑刪除某行--關鍵程式碼
阿新 • • 發佈:2019-02-14
步驟:
一 . 允許刪除的row或者Section返回YES,否則返回NO。
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { if (_ljTableView == tableView) { //刪除狀態下,才允許左滑刪除。 if (_allArray.count > indexPath.section && [_attentionBtn.title isEqualToString:@"刪除"]) { Info *_ljInfo = _allArray[indexPath.section]; if ([_ljInfo.Lineadd isEqualToString:@"1"] || [_ljInfo.Lineaddhint isEqualToString:@"1"]) { return NO; } return YES;//允許編輯 } } return NO;//不允許編輯 }
二. 具體刪除步驟
1. 先移除tableView中陣列的當前刪除行
[_allArray removeObjectAtIndex:indexPath.section];
2. deleteSections(刪除某一個Section) 或者 deleteRowsAtIndexPaths(刪除某一Row)
// Delete the Sections from the data source.
[tableViewdeleteSections:[NSIndexSet indexSetWithIndex:indexPath.section]withRowAnimation :UITableViewRowAnimationAutomatic];
// Delete the row from the data source.
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationFade];
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (_ljTableView == tableView) { if (editingStyle == UITableViewCellEditingStyleDelete) { if(_allArray.count > indexPath.section) { Info *_info = _allArray[indexPath.section]; [_allArray removeObjectAtIndex:indexPath.section];//1.先移除陣列中的當前行 // Delete the deleteSections from the data source.//2.直接刪除tableView中的當前行 [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic]; // Delete the deleteRowsAtIndexPaths from the data source. //- (void)deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation } } } }