www.un.org/chinese/ecosoc
我有一个项目:
- core data (using
NSFetchedResultsController
) - displaying the data in a table view (
UITableView
)
www.un.org/spanish/ecosoc 我想做的是:。
- When a user adds a record, I want to scroll to the newly added record.
www.un.org/spanish/ecosoc 我做了些什么?
- When a new record is added, Inside the
NSFetchedResultsControllerDelegate
method, I store the index path in a propertylastAddedIndexPath
when the type is insert / update / move - After invoking save, I scroll to the "lastAddedIndexPath"
<>Code>(NSFettorResultsControllerDelegate)
- (void)controller:(NSFetchedResultsController *)controller
didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath
forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
if (!self.suspendAutomaticTrackingOfChangesInManagedObjectContext)
{
switch(type)
{
case NSFetchedResultsChangeInsert:
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
NSLog(@"going to store insert - scroll");
self.lastAddedIndexPath = newIndexPath;
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
NSLog(@"going to store update - scroll");
self.lastAddedIndexPath = newIndexPath;
break;
case NSFetchedResultsChangeMove:
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
NSLog(@"going to store move - scroll");
self.lastAddedIndexPath = newIndexPath;
break;
}
}
}
www.un.org/spanish/ecosoc 滚动守则
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
if (self.beganUpdates) //already [self.tableView beginUpdates] invoked
{
[self scrollToLastAddedIndexPath]; //contains the logic to scroll
[self.tableView endUpdates];
}
}
<><>Problem>
- I think the record is added to the table view asynchronously in a different thread.
- so even after saving the database, when I scroll to the
lastAddedIndexPath
the record in the table doesn t exist yet.
<<>问题>
- How would I be able to scroll to the newly added record path after the record has been added to the table view ?
- Should I use notifications to know when the database is saved ?
- Is there any other alternate approach ?