gpt4 book ai didi

ios - 随着新内容的出现滚动到底部

转载 作者:行者123 更新时间:2023-11-28 07:49:56 25 4
gpt4 key购买 nike

所以我有一个 UICollectionView,它在我的应用程序中包含一个聊天功能。当用户发送新消息时,他们会通过观察者模式进入应用程序。但是,当有新内容出现时。它最终位于 keyboard 下方,并且永远不会自行调整。当我切换 keyboard 时,我有一个类似这样的功能,它似乎可以实现这个目标。然而,当我在我的 completion handler 中尝试类似的代码时,它失败了并给我这条消息。

'NSInvalidArgumentException', reason: 'attempt to scroll to invalid index path: {length = 2, path = 0 - 9}' *** First throw call stack:

fileprivate func tryObserveComments(){
print(eventKey)
commentHandle = ChatService.observeMessages(forChatKey: eventKey) { (ref, newComments) in
self.commentRer = ref
self.comments.append(newComments!)
self.adapter.performUpdates(animated: true)
let item = self.collectionView.numberOfItems(inSection: self.collectionView.numberOfSections - 1) - 1
let insertionIndexPath = IndexPath(item: item, section: self.collectionView.numberOfSections - 1)
self.collectionView.scrollToItem(at: insertionIndexPath, at: UICollectionViewScrollPosition.top, animated: true)
}
}

也出于某种原因,在奇怪的情况下它确实有效。一些 keyboard 部分覆盖了单元格

最佳答案

尝试使用 performBatchUpdates,我认为它会像您异常(exception)的那样工作

 fileprivate func tryObserveComments(){
print(eventKey)
commentHandle = ChatService.observeMessages(forChatKey: eventKey) { (ref, newComments) in
self.commentRer = ref
self.comments.append(newComments!)
self.adapter.performUpdates(animated: true)
self.collectionView.performBatchUpdates({
let item = self.collectionView.numberOfItems(inSection: self.collectionView.numberOfSections - 1) - 1
let insertionIndexPath = IndexPath(item: item, section: self.collectionView.numberOfSections - 1)
}, completion: { (Bool) in
self.collectionView.scrollToItem(at: insertionIndexPath, at: UICollectionViewScrollPosition.top, animated: true)
})
}
}

关于ios - 随着新内容的出现滚动到底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49992177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com