我有一个 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/