I have a UITableView
with a single section and 'n' rows (populated from an NSMutableArray
, myTableArray
). When entering Edit mode, I would like to achieve the following:
- setup the "n" rows to have the
UITableViewCellEditingStyleDelete
editing style - insert a new row, above the original 'n', and set it up to have the
UITableViewCellEditingStyleInsert
editing style
My UITableViewController
subclass is implemented as follows:
- (void)setEditing:(BOOL)flag animated:(BOOL)animated
{
[super setEditing:flag animated:animated];
UITableView *tableView = (UITableView *)self.view;
NSArray *topIndexPath = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]];
if (self.editing == YES)
[tableView insertRowsAtIndexPaths:topIndexPath withRowAnimation:UITableViewRowAnimationBottom];
else
[tableView deleteRowsAtIndexPaths:topIndexPath withRowAnimation:UITableViewRowAnimationBottom];
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0)
return UITableViewCellEditingStyleInsert;
else
return UITableViewCellEditingStyleDelete;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.editing == YES)
return [myTableArray count] + 1;
else
return [myTableArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// ... boiler-plate dequeueReusableCellWithIdentifier code, etc ...
if (self.editing == YES)
{
if (indexPath.row == 0)
cell.textLabel.text = @"My new row";
else
cell.textLabel.text = [myTableArray objectAtIndex:(indexPath.row - 1)];
}
else
{
cell.textLabel.text = [myTableArray objectAtIndex:indexPath.row];
}
return cell;
}
When this code is run, and the Edit button is pressed, a new row is inserted above the original 'n' rows, however, the second row, in addition to the first, has the UITableViewCellEditingStyleInsert
editing style. For example, if 'n' is 3, after the Edit button is pressed the table displays:
- row 0: insert editing style
- row 1: insert editing style (should be delete)
- row 2: delete editing style
- row 3: delete editing style
After further investigation, I've noticed that if 'n' is 3 and the Edit button is pressed, the method tableView:editingStyleForRowAtIndexPath:
is called a total of 8 times:
[super setEditing:flag animated:animated]
results in 3 calls for each of the original rows 0, 1 and 2- the subsequent
[tableView insertRowsAtIndexPaths:topIndexPath withRowAnimation:UITableViewRowAnimationBottom]
results in 5 calls for rows 0, 1, 2, 3, and then 0 again
I can understand that the editing style needs to be re-corrected as a result of inserting a new row, however I do not understand why in the last 5 calls, row 0 is set twice and why row 1 still manages to have the incorrect style.
Does anyone have any insight?