简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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