简体   繁体   English

在将新数据插入核心数据存储区时,如何使UICollectionView始终位于底部

[英]How to keep UICollectionView always at the bottom while new data is inserted into core data store

I have a UICollectionView that uses an NSFetchedResultsController and I would like to have the view always scrolled at the bottom as new entries are inserted into the persistent store. 我有一个使用NSFetchedResultsController的UICollectionView,并且当新条目插入到持久性存储中时,我希望视图始终在底部滚动。 How would I do this? 我该怎么做? I have implemented the NSFetchedResultsController delegate methods and viewing the data in the collection works nice but I'm not sure how to implement this additional requirement. 我已经实现了NSFetchedResultsController委托方法,并且可以很好地查看集合中的数据,但是我不确定如何实现这一额外要求。 Suggestions? 有什么建议吗?

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    [yourCollectionView reloadData];

    CGRect lastCell = CGRectMake(0, CGFLOAT_MAX, 50, 50);

    [yourCollectionView scrollRectToVisible:lastCell animated:YES];

    //Or        
    /*
     NSIndexPath *lastRow = [NSIndexPath indexPathForItem:yourItem inSection:yourSection];

    [collectionView scrollToItemAtIndexPath:lastRow
                           atScrollPosition:UICollectionViewScrollPositionBottom
                                   animated:NO];
   */
}

Try 尝试

-(void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated;

This will serve your purpose. 这将达到您的目的。

You actually need to do three things: 您实际上需要做三件事:

  1. Make sure your fetched results controller sorts by created date ascending. 确保您的提取结果控制器按创建日期升序排序。
    (You might need an attribute for that.) (您可能需要一个属性。)
  2. In the delegate method, insert the new object at the correct index path. 在委托方法中,将新对象插入正确的索引路径。
  3. If necessary, scroll to the item so it is visible. 如有必要,滚动到该项目以使其可见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM