Best way to select a row after inserting it?

I am working on an iOS application using a CoreData-based navigation template. I would like to highlight and "scroll to the visible" row after it has been inserted into the table view . Ideally, I would like to select it, deselect it and select it again to get some kind of blinking effect.

Since I use this method, the template proves, namely:

#pragma mark - Fetched results controller delegate - (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { [self.tableView beginUpdates]; } - (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type { switch(type) { case NSFetchedResultsChangeInsert: [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; break; case NSFetchedResultsChangeDelete: [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; break; } } - (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:UITableViewRowAnimationTop]; [self.tableView selectRowAtIndexPath:newIndexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle]; break; case NSFetchedResultsChangeDelete: [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; break; case NSFetchedResultsChangeUpdate: [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath]; break; case NSFetchedResultsChangeMove: [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationFade]; break; } } - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { [self.tableView endUpdates]; } 

I'm a little confused and don't know where to put this selection code.
If I put [tableView selectRowAtIndexPath:<#(NSIndexPath *)#> animated:<#(BOOL)#> scrollPosition:<#(UITableViewScrollPosition)#>]
in
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller ,
he selects a line, but immediately cancels it, and scrolling does not behave as it should.

+8
ios uitableview core-data
source share
4 answers

in the controller:didChangeObject:atIndexPath:forChangeType:newIndexPath: change this section:

 case NSFetchedResultsChangeInsert: [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; [tableView scrollToRowAtIndexPath:newIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; insertedIndexPath = newIndexPath; //remember for selection break; 

and add a method from UIScrollViewDelegate:

 - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView { [self.tableView selectRowAtIndexPath:insertedIndexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; } 

don't forget to add the insertIndexPath variable.

+3
source share

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!

+3
source share

Basically the same answer as above, but with fixed placement when to call scrollToRowAtIndexPath

in the controller:didChangeObject:atIndexPath:forChangeType:newIndexPath: change this section:

 case NSFetchedResultsChangeInsert: [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; //save new index path into your own ivar self.newlyInsertIndex = newIndexPath break; 

if the boiler plate FRC code is used, it cannot scroll to the newly inserted index until the table update is completed

Do this in the method - (void)controllerDidChangeContent:

  if (!self.changeIsUserDriven){ [self.tableView endUpdates]; if (self.newlyInsertedIndex){ //ScrollPositionNone == use least amount of effort to show cell [self.tableView scrollToRowAtIndexPath:self.newlyInsertedIndex atScrollPosition:UITableViewScrollPositionNone animated:YES]; //cleanup self.newlyInsertedIndex = Nil; } } 

and add a method from UIScrollViewDelegate:

 - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView { [self.tableView selectRowAtIndexPath:insertedIndexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; } 

don't forget to add newInsertedIndex ivar

+2
source share

All this did not help me. I have done this:

  • when I create a new object, I remember it in the _newInstance instance _newInstance
  • when the object is saved or the creation is canceled, I clear it: _newInstance = nil;
  • in controllerDidChangeObject I select the line:

     - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { [self.tableView endUpdates]; if (_newObject) { NSIndexPath *ip = [self.fetchedResultsController indexPathForObject:_newObject]; [self.tableView selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionMiddle]; } } 
0
source share

All Articles