美文网首页
iOS开发 tableview左滑删除(两种),浅记一下~

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

作者: 小y想吃糖 | 来源:发表于2022-06-14 14:19 被阅读0次

方法一、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<UITableViewRowAction*>*)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左滑删除(两种),浅记一下~

      本文链接:https://www.haomeiwen.com/subject/zjjzmrtx.html