iPhone delete messages like interface
-
12-12-2019 - |
Question
I want to delete rows in a UITableView, I need to provide the delete button not by swipe but in a way similar to delete messages functionality in iPhones. In which on clicking edit a small red circle appears on the left in the cell which rotates when clicked and the delete button is shown. How to implement this?
Like in the image below:
Solution
See the Apple documentation here for details on how to achieve this. In particular:
When sent a
setEditing:animated
: message (with a first parameter ofYES
), the table view enters into editing mode where it shows the editing or reordering controls of each visible row, depending on theeditingStyle
of each associatedUITableViewCell
. Clicking on the insertion or deletion control causes the data source to receive atableView:commitEditingStyle:forRowAtIndexPath
: message. You commit a deletion or insertion by callingdeleteRowsAtIndexPaths:withRowAnimation
: orinsertRowsAtIndexPaths:withRowAnimation
:, as appropriate.
OTHER TIPS
write this self.navigationItem.leftBarButtonItem = self.editButtonItem;
in viewDidLoad
method. After that implement this method - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[yourArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
}
}