views:

695

answers:

1

This is really driving me crazy.

I have a table view that displays a sorted list of customers. Users can add a new customer, thus I have to add a new row to the table view (by updating the data model and calling insertRowsAtIndexPaths:withRowAnimation:). However, since the table view is sorted, this insert can occur off-screen, which is not very nice wrt. user experience. So my idea was to scroll the table view to the index path where the insert will occur before actually inserting the row:

- (void)finishedSavingNewCustomer:(Customer*)customer atIndex:(NSInteger)index {
    // NOTE: self.customers (which is the model for the table view used in all datasource
    // methods) has already been updated at this point, ie. it already contains the new customer

    // scroll the table view so that the insert position is on-screen
    NSInteger scrollRow = (index < ([self.customers count] - 1) ? index : [self.customers count] - 2);
    NSIndexPath* scrollIndexPath = [NSIndexPath indexPathForRow:scrollRow inSection:0];
    [self.tableView scrollToRowAtIndexPath:scrollIndexPath atScrollPosition:UITableViewScrollPositionNone animated:NO];

    // insert the new row
    NSArray* indexPaths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:index inSection:0]];
    [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
}

This code actually works as expected: The table view scrolls and the new row is inserted correctly.

However, after the insert some table view cells become "irresponsive", ie. they won't display the delete button upon swipe and don't even highlight when selected. Since this is really weird and kinda hard to explain, let me try to illustrate it.

Initial situation: Rows 1 - 5 are displayed:

--------- screen top --------
Row 1
Row 2
Row 3
Row 4
Row 5
------- screen bottom -------
Row 6
Row 7
Row 8 
Row 9

Situation after scrolling and inserting:

Row 1
Row 2
Row 3
Row 4
Row 5
--------- screen top --------
Row 6
Row 7
Row 7a  << newly inserted
Row 8 
Row 9
------- screen bottom -------

Now, after the insert rows 1 - 5 will not respond to user interaction as described above.

Anybody ever experienced this kind of problem? Ideas? Are there other approaches to make sure that the insertion of new rows is visible for the user?


Update: Just tested on the device, and it turns out that this problem only occurs on the simulator! This makes the issue less unpleasant, but I'd still be interested if someone else has experienced this behavior.

+1  A: 

I had some similar experiences with the Simulator recently - I'm dealing with an application driving several table views from Core Data, and it also presents a bunch of UIAlertViews and UIActionSheets at various points through the application.

I've noticed that the Simulator doesn't respond well to touch events right after a UI action (table cell insert, action sheet display, etc.) occurs. It usually helps for me to either

  • switch to another window then back to the Simulator or
  • click and drag the mouse through some touch-inactive area of the Simulator, then try the action I want again

Your mileage may vary. It may be a pain during testing, but as long as everything works properly on the device, you should be fine.

Tim
Your two suggestions didn't work for me, but it is definitely a bug in the simulator. Even reloading the table view won't help! Anyways, thanks for sharing your experience.
Daniel Rinser
Anytime. You may want to file a bug report: https://bugreport.apple.com/
Tim