Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem with editing style when inserting a new row at top of UITableView as part of entering Edit mode
    primarykey
    data
    text
    <p>I have a <code>UITableView</code> with a single section and 'n' rows (populated from an <code>NSMutableArray</code>, <code>myTableArray</code>). When entering Edit mode, I would like to achieve the following:</p> <ul> <li>setup the "n" rows to have the <code>UITableViewCellEditingStyleDelete</code> editing style</li> <li>insert a new row, above the original 'n', and set it up to have the <code>UITableViewCellEditingStyleInsert</code> editing style</li> </ul> <p>My <code>UITableViewController</code> subclass is implemented as follows:</p> <pre><code>- (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; } </code></pre> <p>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 <code>UITableViewCellEditingStyleInsert</code> editing style. For example, if 'n' is 3, after the Edit button is pressed the table displays:</p> <ul> <li>row 0: insert editing style</li> <li>row 1: insert editing style (should be delete)</li> <li>row 2: delete editing style</li> <li>row 3: delete editing style</li> </ul> <p>After further investigation, I've noticed that if 'n' is 3 and the Edit button is pressed, the method <code>tableView:editingStyleForRowAtIndexPath:</code> is called a total of 8 times:</p> <ul> <li><code>[super setEditing:flag animated:animated]</code> results in 3 calls for each of the original rows 0, 1 and 2</li> <li>the subsequent <code>[tableView insertRowsAtIndexPaths:topIndexPath withRowAnimation:UITableViewRowAnimationBottom]</code> results in 5 calls for rows 0, 1, 2, 3, and then 0 again</li> </ul> <p>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.</p> <p>Does anyone have any insight?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload