February 2012
Intermediate to advanced
872 pages
22h 43m
English
You want your application users to be able to delete rows from a table view easily.
Implement the tableView:editingStyleForRowAtIndexPath:
selector in the delegate and the tableView:commitEditingStyle:forRowAtIndexPath:
selector in the data source of your table view:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCellEditingStyle result = UITableViewCellEditingStyleNone;
if ([tableView isEqual:self.myTableView]){
result = UITableViewCellEditingStyleDelete;
}
return result;
}
- (void) setEditing:(BOOL)editing
animated:(BOOL)animated{
[super setEditing:editing
animated:animated];
[self.myTableView setEditing:editing
animated:animated];
}
- (void) tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete){
if (indexPath.row < [self.arrayOfRows count]){
/* First remove this object from the source */
[self.arrayOfRows removeObjectAtIndex:indexPath.row];
/* Then remove the associated cell from the Table View */
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationLeft];
}
}
}The tableView:editingStyleForRowAtIndexPath: method can enable deletions. It is called by the table view and its return value determines what the table view allows the user to do ...
Read now
Unlock full access