I solved a similar problem. I inserted an element at the beginning of the table, and I wanted to select it right after.
Step 1. - Create a radio button to save the state if it should select the first row
@implementation MyTableViewController { BOOL _selectFirstRow; }
Step 2. - Switch it to YES when pasting
- (void) controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath { UITableView *tableView = self.tableView; switch (type) { case NSFetchedResultsChangeInsert: [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; _selectFirstRow = YES; break;
Step 3. - Create a method to select the first row
- (void) selectFirstTableRow { [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop]; }
Step 4. - Call the method to select a row immediately after updating the table
- (void) controllerDidChangeContent:(NSFetchedResultsController *)controller { [self.tableView endUpdates]; if (_selectFirstRow) { [self selectFirstTableRow]; _selectFirstRow = NO; } }
To all people .. if you want to select a different row, also store newIndexPath in the class parameter. All this. Hope this helps you. Cia!
Lweek
source share