Iphone 使用ScrollToRowatineXpath:atScrollPosition:AnimatedUsingnsFetchedResultsControllerDelegate方法滚动到最后一个UITableViewCell
我正在将一个新项目添加到UITableView的底部,插入该项目后,我希望UITableView滚动到最底部以显示新插入的项目。新项目将保存到核心数据,UITableView将使用NSFetchedResultsController自动更新Iphone 使用ScrollToRowatineXpath:atScrollPosition:AnimatedUsingnsFetchedResultsControllerDelegate方法滚动到最后一个UITableViewCell,iphone,ios,uitableview,nsfetchedresultscontroller,Iphone,Ios,Uitableview,Nsfetchedresultscontroller,我正在将一个新项目添加到UITableView的底部,插入该项目后,我希望UITableView滚动到最底部以显示新插入的项目。新项目将保存到核心数据,UITableView将使用NSFetchedResultsController自动更新 - (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath
forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
switch (type) {
case NSFetchedResultsChangeInsert:
NSLog(@"*** controllerDidChangeObject - NSFetchedResultsChangeInsert");
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
//THIS IS THE CODE THAT DOESN'T WORK
[self.tableView scrollToRowAtIndexPath:newIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
break;
....
}
这导致了一个越界错误,我似乎无法使它工作。通过调整索引路径的行,我可以滚动到倒数第二条注释,但我无法进入最后一项
基本上,我正在向注释表添加注释,添加注释后,我希望该表滚动到最近的注释 看看这是否有帮助
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
[self.tableView scrollToRowAtIndexPath:newIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
您需要调用
endUpdates
,以便tableView
可以计算其新的节和行。一个简单的例子如下所示:
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:insertedIndexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
[self.tableView scrollToRowAtIndexPath:insertedIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
当您使用NSFetchedResultsController
时,它有点复杂,因为调用beginUpdate
,insertRowsAtIndexPaths:withRowAnimation:
和endUpdates
通常采用不同的委托方法。那么你能做的就是
insertedIndexPath
以存储插入的索引路径-controller:didChangeObject:atindexpaths:withRowAnimation:
后,添加
self.insertedIndexPath = insertedIndexPath;
-controllerDidChangeContent:
中的[self.tableView endUpdates]
之后,添加
if (self.insertedIndexPath) {
[self.tableView scrollToRowAtIndexPath:self.insertedIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
self.insertedIndexPath = nil;
}
创建名为“newIndexPath”的属性将生成生成错误:
属性遵循Cocoa命名约定返回“拥有的”对象
只需将属性命名为不同的名称即可::D这是在编译器没有对此抱怨的时候编写的。我将把newIndexPath
更改为insertedIndexPath
。