Left slide function
Left stroke Delete
1.实现UITableViewDelegate协议和代理2.实现左划删除功能和修改按钮文字的代理方法注意:此时按钮没有反应,下面第一个方法可以实现对按钮的监听事件,就可以做出操作
/** * 重写这个方法,就可以实现左划删除功能 */- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ // 删除模型数组第一个东西 // 1.修改模型 [self.wineArray removeObjectAtIndex:indexPath.row]; // 2.刷新数据 [self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationAutomatic];}/** * 重写实现左划删除为中文格式,可以直接设置(下面附图) */- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{ return @"删除";}
Custom Left Stroke button
> as above, implement TableView protocol and proxy > implement TableView corresponding proxy method Note: The following method is implemented, then the above modified text function is not implemented, After iOS9 the first method is also not implemented, the left row appears button TableView into the editing mode, so that it exits the left stroke directly set exit edit mode self.tableview.editing = NO;
/** * Custom left-shifted button */-(nsarray<uitableviewrowaction *> *) TableView: (UITableView *) TableView Editactionsforrowati Ndexpath: (Nsindexpath *) indexpath{self.tableView.editing = YES; Uitableviewrowaction *action1 = [uitableviewrowaction rowactionwithstyle:uitableviewrowactionstyledefault title:@ " Delete "handler:^ (uitableviewrowaction * _nonnull action, Nsindexpath * _nonnull indexpath) {/************** Delete function * * * * * * * ///modify model [Self.winearray Removeobject:self.winearray[indexpath.row]]; Refresh table [Self.tableview Deleterowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationautomatic]; }]; Uitableviewrowaction *action2 = [uitableviewrowaction rowactionwithstyle:uitableviewrowactionstylenormal title:@ "concerns "Handler:^ (uitableviewrowaction * _nonnull action, Nsindexpath * _nonnull indexpath) {/************ exit edit mode ****** /self.tableView.editing = NO; }]; Return @[acTion1, Action2]; The more you add it, the closer the button to the right}
Multi-Select Case
Multi-Select
self.tableView.allowsMultipleSelection = YES;
Multi-Select in edit mode
self.tableView.allowsMultipleSelectionDuringEditing = YES;
Case: To bulk Delete, then delete whole
self.tableview.indexPathsForSelectedRows // 可以保持选中的行 注意:数组不能遍历的同时删除,会删错东西
Code:
- (void)viewDidLoad { [super viewDidLoad]; // 编辑模式下的多选 self.tableView.allowsMultipleSelectionDuringEditing = YES;}
/** * 批量删除 */- (IBAction)deleteAll { // 实现点击换button功能 /**** 根据编辑模式设置可选状态 ****/ [self.tableView setEditing:!self.tableView.editing animated:YES]; self.deleteAllBtn.selected = self.tableView.editing;}/** * 删除 */- (IBAction)delete { /***** 1.找出要删除的行 *****/ NSMutableArray *deleteArray = [NSMutableArray array]; // self.tableView.indexPathsForSelectedRows 选中的行 NSArray *rowsArray = self.tableView.indexPathsForSelectedRows; for (NSIndexPath *indexPath in rowsArray) { [deleteArray addObject:self.wineArray[indexPath.row]]; } /***** 2.修改模型 *****/ [self.wineArray removeObjectsInArray:deleteArray]; /***** 3.刷新表格 *****/ [self.tableView deleteRowsAtIndexPaths:rowsArray withRowAnimation:UITableViewRowAnimationAutomatic];}
Left slide function and multi-select function (functions used in some lists)