use*_*037 2 core-data uitableview nsfetchedresultscontroller ios
概述:
我有一个iOS项目,其中包括:
NSFetchedResultsController)UITableView)我想做的事:
我做了什么
NSFetchedResultsControllerDelegate方法内部,lastAddedIndexPath当类型为insert/update/move时,我将索引路径存储在属性中代码(NSFetchedResultsControllerDelegate)
- (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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
要滚动的代码
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
if (self.beganUpdates) //already [self.tableView beginUpdates] invoked
{
[self scrollToLastAddedIndexPath]; //contains the logic to scroll
[self.tableView endUpdates];
}
}
Run Code Online (Sandbox Code Playgroud)
问题
lastAddedIndexPath表中的记录时还不存在.问题
您的滚动必须发生在此之后,[self.tableView endUpdates]因为新行未添加到表中.所以切换两个语句:
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
if (self.beganUpdates) //already [self.tableView beginUpdates] invoked
{
[self.tableView endUpdates];
[self scrollToLastAddedIndexPath]; //contains the logic to scroll
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2319 次 |
| 最近记录: |