簡體   English   中英

在將新數據插入核心數據存儲區時,如何使UICollectionView始終位於底部

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

我有一個使用NSFetchedResultsController的UICollectionView,並且當新條目插入到持久性存儲中時,我希望視圖始終在底部滾動。 我該怎么做? 我已經實現了NSFetchedResultsController委托方法,並且可以很好地查看集合中的數據,但是我不確定如何實現這一額外要求。 有什么建議嗎?

- (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];
   */
}

嘗試

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

這將達到您的目的。

您實際上需要做三件事:

  1. 確保您的提取結果控制器按創建日期升序排序。
    (您可能需要一個屬性。)
  2. 在委托方法中,將新對象插入正確的索引路徑。
  3. 如有必要,滾動到該項目以使其可見。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM