ios - UICollectionView 滚动到没有动画的最后一项

标签 ios objective-c scroll uicollectionview

我有一个 UICollectionView 来显示聊天消息。一开始我将现有消息加载到 collectionView 并使用此方法将 collectionView 向下滚动到最后一条消息:

- (void)scrollToLastMessageAnimated:(BOOL)animated;
{
    if (_messages.count == 0) { return; }

    NSUInteger indexOfLastSection = _messagesBySections.count - 1;
    NSInteger indexOfMessageInLastSection = [_messagesBySections[indexOfLastSection] count] - 1;
    NSIndexPath *path = [NSIndexPath indexPathForItem:indexOfMessageInLastSection
                                            inSection:indexOfLastSection];

    [_collectionView scrollToItemAtIndexPath:path
                           atScrollPosition:UICollectionViewScrollPositionCenteredVertically
                                   animated:animated];
}

只有当我在 viewDidAppear: 方法中而不是在 viewWillAppear: 方法中调用它时,它才会以动画方式运行。如何在没有动画的情况下向下滚动?

最佳答案

这里是...

NSInteger section = [self.collectionView numberOfSections] - 1;
NSInteger item = [self.collectionView numberOfItemsInSection:section] - 1;
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:item inSection:section];
[self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:(UICollectionViewScrollPositionBottom) animated:YES];

关于ios - UICollectionView 滚动到没有动画的最后一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23997939/

相关文章:

ios - 无法将 Lottie 动画 View 添加到 Collection View 单元格

jquery - CSS3 动画 - 如何为滚动上的不同元素制作不同的动画

javascript - JQuery Double Scroll - 边距/对齐问题

ios - 如何将 UICollectionView 添加到 inputAccessoryView

ios - iPhone 上的 UISplitViewController : pop to/show primary view from detail view controller

iOS:目标如何一次获得一张图像发光,因为手指被拖过一系列图像中的每一个

objective-c - 如何在系统启动时启动应用程序? (Mac OSX, cocoa )

objective-c - 如何从 AFNetworking 和 AFJSONRequestOperation 获取可变字典?

ios - RestKit 对象映射将一个 ID 映射为一个完全不同的负数

CSS:只滚动到可见内容