Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 使用ScrollToRowatineXpath:atScrollPosition:AnimatedUsingnsFetchedResultsControllerDelegate方法滚动到最后一个UITableViewCell_Iphone_Ios_Uitableview_Nsfetchedresultscontroller - Fatal编程技术网

Iphone 使用ScrollToRowatineXpath:atScrollPosition:AnimatedUsingnsFetchedResultsControllerDelegate方法滚动到最后一个UITableViewCell

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

我正在将一个新项目添加到UITableView的底部,插入该项目后,我希望UITableView滚动到最底部以显示新插入的项目。新项目将保存到核心数据,UITableView将使用NSFetchedResultsController自动更新

- (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