且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用滑动手势删除 tableview 上的行

更新时间:2023-02-07 08:01:07

你可以通过实现以下 UITableView 委托方法来做到这一点

You can do this by implementing following UITableView deligate methods

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleNone; //if you don't want to show delete button
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
        return YES; // allow that row to swipe
        //return NO; // not allow that row to swipe
}

// Override to support editing/deleteing the table view cell on swipe.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleNone)
    {
        //--- your code for delete -----
    }
}