Reputation: 2121
I have a tableviewController
. On the UINavigationBar
i will be adding a button called Edit. When the user clicks on it i need all the cells to go for the edit mode, where they could delete records.
I guess the following method does it. Am i correct ? will i get the red circle and the delete button on the cell when i click the edit button?
2.) How can i write the code for the Edit button (UIBarbuttonitem
), when the user clicks it to call the following method ?
3.) When we delete the cell, i need to reload the data in the table. I wrote the code for this, is it correct. (I am not in my Mac at the moment)
- (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];
[self.tableView beginUpdates];
[discardedItems addObject:[self.entries objectAtIndex:indexPath.row]];
[self.itemsMutableArray removeObjectsInArray:discardedItems ];
self.entries = [NSArray arrayWithArray:self.itemsMutableArray];
[self.tableView endUpdates];
[self.tableView reloadData];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
Upvotes: 0
Views: 258
Reputation: 1715
First of all, you need to specify if the rows can be edited. This is done by the following method
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
return yes if you want all the rows to be editable.
To get the red circles use this,
Perhaps in a method called by the edit button...
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Edit ", @"") style:UIBarButtonItemStyleBordered target:self action:@selector(pressedEdit:)];
self.navigationItem.leftBarButtonItem = editButton;
(in viewdidload)
and in pressedEdit:
- (void) pressedEdit :(id)sender {
UIBarButtonItem *editButton = (UIBarButtonItem*)self.navigationItem.leftBarButtonItem;
if (!self.tabeView.editing) {
[self.tableView setEditing:YES animated:YES];
editButton.title = NSLocalizedString(@"Done", @"");
}
else {
[self.tableView setEditing:NO animated:YES];
editButton.title = NSLocalizedString(@"Edit", @"");
}
}
According to the code youve written, i think you should first update the datasource and then remove the cell...
Upvotes: 1
Reputation: 90117
Your code does not seem wrong. But so many lines for so little work. You kill the animation with reloadData too. And you wrap calls that do not even touch the tableView in beginUpdates/endUpdates.
Why not use a NSMutableArray as datasource in the first place? You can reduce seven lines of code turned to two:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// delete the item from the datasource
[self.entries removeObjectAtIndex:indexPath.row];
// Delete the row from the table view
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
Upvotes: 1