iOS开发 tableview左滑删除(两种),浅记一下~

方法一、iOS8-iOS10

#pragma mark 左滑删除 iOS8-iOS10

- (BOOL)tableView:(UITableView*)tableViewcanEditRowAtIndexPath:(NSIndexPath*)indexPath {

    return YES;

}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {

    return UITableViewCellEditingStyleDelete;

}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {

    return@"删除";

}

// 自定义左滑显示编辑按钮

- (NSArray*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {

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

        //删除操作

    }];

    deleteAction.backgroundColor= [UIColorcolorWithHexString:@"#fa514a"];

    return@[deleteAction];

}


方法二、iOS11以上

#pragma mark左滑删除 iOS11以上

- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath*)indexPath  API_AVAILABLE(ios(11.0)) {

    //删除

    CHMessageChatModel*model =self.dataList[indexPath.row];

    UIContextualAction*deleteRowAction = [UIContextualActioncontextualActionWithStyle:UIContextualActionStyleDestructivetitle:@"删除"handler:^(UIContextualAction*_Nonnullaction,__kindofUIView*_NonnullsourceView,void(^_NonnullcompletionHandler)(BOOL)) {

        //左滑删除之后数据处理操作

        [self deleteMessageWithReceiveCode:model.receiveCode atIndexPath:indexPath];

    }];

//    deleteRowAction.image = [UIImage imageNamed:@"ceshi.png"];//给删除赋值图片

    deleteRowAction.backgroundColor= [UIColorcolorWithHexString:@"#fa514a"];//删除背景颜色

    UISwipeActionsConfiguration *Configuration = [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction]];

    returnConfiguration;

}

你可能感兴趣的:(iOS开发 tableview左滑删除(两种),浅记一下~)