0

I have a tableView in my view and all works fine, however each custom cell (ToDoListCell) has a text field which I allow users to edit, updating their data. All good and well, my problem occurs whenever trying to scroll to a cell which is going to be hidden by the keyboard.

Note: I adjust the size of the table using the NSNotificationCenter observer methods as the Apple documentation recommends and I know this is not the problem.

- (void)scrollToSelectedCell {

        NSIndexPath *currentIndexPath = [NSIndexPath indexPathForItem:0 inSection:0];
        while (currentIndexPath.row < self.todos.count) {
           ToDoListCell *cell = (ToDoListCell *)[self.table cellForRowAtIndexPath:currentIndexPath];
                if (cell.titleField.isEditing == YES) {
                      [self.table scrollToRowAtIndexPath:currentIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
                      break;
                }

           currentIndexPath = [NSIndexPath indexPathForItem:currentIndexPath.row + 1 inSection:0];

         }
}

Any idea why it does not scroll to any cell which is not currently on screen?

Thanks,

Ben

Ben Toogood
  • 469
  • 4
  • 9

2 Answers2

0

Just a guess, but probably because

ToDoListCell *cell = (ToDoListCell *)[self.table cellForRowAtIndexPath:currentIndexPath];

is returning nil causing cell.titleField.isEditing == YES to always fail. So the scroll command is never called. Table views only hold onto visible cells, so if you've resized your table view such that the cell being edited is no longer visible, you're going probably going to get nil back. You need to determine the index path of the cell being edited before resizing the table.

Timothy Moose
  • 9,895
  • 3
  • 33
  • 44
0

If you are firing the code in response to a notification listener, are you firing the notification from the UI thread?

UI animations will not perform unless executed on the main thread.

dispatch_async(dispatch_get_main_thread(), ^{
   // send notification
});
Lytic
  • 786
  • 5
  • 12