简体   繁体   中英

UITableViewCell delete button without editing mode (like mail app)

Not sure if this functionality is built into UITableViewCell , but I'd like to mimic the behavior of the mail app. I have a UITableView with dynamic cells. When the user performs a left swipe gesture, a delete button (I believe the button text in the mail app says archive instead of delete) should appear on the right-hand side of the cell.

I'm confortable with what to do after that to delete the row. I just need to make the button appear and disapper should the user swipe right OR tap anywhere else in the UITableView .

I would prefer to do this without adding an 'Edit' UINavigationItem button and putting the tableView in edit mode as per the "Table View Programming Guide", but appreciate the use of any built in methods.

Thanks.

The -tableView:canEditRowAtIndexPath: delegate won't cause the button to display on swipe. You use it to determine which rows you want to allow to be edited (deleted) and can just leave it unimplemented if you want to allow all rows to be edited. To get the delete button to show up when you swipe without putting the table view in edit mode, however, you need to implement:

- (void)tableView:(UITableView *)tableView 
                  commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
                   forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Get the object you want to remove from your 
        // collection and delete it here.
    }
}

Add the UITableViewDelegate method -tableView:canEditRowAtIndexPath: to your controller. Return YES for rows that can be deleted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM