刚开始,开发一个新功能,需要用到左滑编辑,删除功能。代码如下:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
//第二组可以左滑删除
if (indexPath.section == 0) {
return YES;
}
return NO;
}
/**
* 只要实现了这个方法,左滑出现按钮的功能就有了
* (一旦左滑出现了N个按钮,tableView就进入了编辑模式, tableView.editing = YES)
*/
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
}
/**
* 左滑cell时出现什么按钮
*/
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
__weak typeof(self) weakSelf = self;
UITableViewRowAction *actionEdit = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"编辑" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"点击了编辑");
// 收回左滑出现的按钮(退出编辑模式)
tableView.editing = NO;
[weakSelf editSubscribeInfoAction:indexPath];
}];
NSInteger row = indexPath.row;
NSString *title = @"显示";
if (self.dataArray.count > row) {
LFGetSubScribeByListOutputModel *model = self.dataArray[row];
if ([model.state isEqualToString:@"0"]) {
title = @"隐藏";
}
}
UITableViewRowAction *actionHide = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:title handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"点击了隐藏");
// 收回左滑出现的按钮(退出编