editActionsForRowAtIndexPath(iOS8) tableview编辑(删除、插入、移动)

时间:2023-03-09 20:29:54
editActionsForRowAtIndexPath(iOS8)  tableview编辑(删除、插入、移动)

ios8 出来的左滑小菜单 可以自定义想要的按钮 (要求ios8以上)

- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {

        NSLog(@"-----------=点击删除");

    }];

    UITableViewRowAction *editAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"编辑" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {

        NSLog(@"-----------=点击编辑");

    }];

    UITableViewRowAction *topAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"置顶" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {

        NSLog(@"-----------=点击置顶");

    }];

    UITableViewRowAction *signAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"标记未读" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {

        NSLog(@"-----------=标记");

    }];

    // 毛玻璃效果
deleteAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]; editAction.backgroundColor = [UIColor blueColor];
topAction.backgroundColor = [UIColor grayColor];
signAction.backgroundColor = [UIColor yellowColor]; return @[deleteAction, editAction, topAction, signAction];
}

可以在导航栏右边放编辑按钮,删除操作

-(void)delete:(UIBarButtonItem *)sender {

    if (self.tableView.editing == NO) {
self.tableView.editing = YES;
sender.title = @"完成";
}else if (self.tableView.editing == YES) {
self.tableView.editing = NO;
sender.title = @"编辑";
} } // 设置tableView是否可以编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
// 设置删除操作时候的标题
-(NSString*)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
return @"删除";
} // 编辑模式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
} -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ if (editingStyle == UITableViewCellEditingStyleDelete) { [self.arrayM removeObjectAtIndex:indexPath.row]; [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone]; } }

导航栏右边放编辑按钮,插入操作

// 设置tableView是否可以编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
} - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleInsert;
} -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ if (editingStyle == UITableViewCellEditingStyleInsert) { // 我们实现的是在所选行的位置插入一行,因此直接使用了参数indexPath
NSArray *insertIndexPaths = [NSArray arrayWithObjects:indexPath,nil];
// 同样,将数据加到list中,用的row
[self.arrayM insertObject:@"新添加的行" atIndex:indexPath.row];
[tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
}
}

导航栏右边放编辑按钮,移动操作

// 设置编辑模式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleNone;
} // 这个方法用来告诉表格 这一行是否可以移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
} // 这个方法就是执行移动操作的
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *) sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { NSUInteger fromRow = [sourceIndexPath row];
NSUInteger toRow = [destinationIndexPath row]; id object = [self.arrayM objectAtIndex:fromRow];
[self.arrayM removeObjectAtIndex:fromRow];
[self.arrayM insertObject:object atIndex:toRow];
}