How to remove data from a table view?
I've set up my navigation bar with an add button that adds data to my table view. I've also added this line self.navigationItem.leftBarButtonItem = self.editButtonItem;
to create the edit button. Looking through the method I notice the - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
method. So I made the modifications below, but my app freezes. What is the proper way to delete data from my table?
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source.
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
**[myArray removeObjectAtIndex:[indexPath row]];
[self.tableView reloadData];**
}
else if (editingStyle == UITableViewCellEditingStyleInse开发者_开发技巧rt) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
Try to remove data from array first, and then notify table to remove corresponding rows. -reloadData call is unnecessary in this situation:
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source.
[myArray removeObjectAtIndex:[indexPath row]];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
精彩评论