我无法滚动到底部,我尝试了其他解决方案,但没有成功。
我正在使用IGLiskit
这是错误:
尝试滚动到无效的索引路径
代码如下:
func initMessages() {
DataService.call.retrieveMessages(roomID: room.id ?? "") { (success, error, messages) in
if !success {
print("error", error!.localizedDescription)
} else {
guard let messages = messages else {return}
self.messages = messages
DispatchQueue.main.async {
self.adapter.performUpdates(animated: true, completion: nil)
let indexPath = IndexPath(item: self.messages.count - 1, section: 0)
self.collectionView.scrollToItem(at: indexPath, at: .bottom, animated: true)
}
}
}
}
答案 0 :(得分:0)
您在collectionview中有2个部分吗?是的,请忽略我的回答,我希望问题是您正在尝试访问第1部分,而您只有第0部分可用,请尝试更改此代码:
func initMessages() {
DataService.call.retrieveMessages(roomID: room.id ?? "") { (success, error, messages) in
if !success {
print("error", error!.localizedDescription)
} else {
guard let messages = messages else {return}
self.messages = messages
DispatchQueue.main.async {
self.adapter.performUpdates(animated: true, completion: nil)
// Here in section
let indexPath = IndexPath(item: self.messages.count - 1, section: 0)
self.collectionView.scrollToItem(at: indexPath, at: .bottom, animated: true)
}
}
}
}
编辑: 好的,如果该部分不起作用,请尝试以下扩展程序:
extension UICollectionView {
func scrollToLast() {
guard numberOfSections > 0 else {
return
}
let lastSection = numberOfSections - 1
guard numberOfItems(inSection: lastSection) > 0 else {
return
}
let lastItemIndexPath = IndexPath(item: numberOfItems(inSection: lastSection) - 1,
section: lastSection)
scrollToItem(at: lastItemIndexPath, at: .bottom, animated: true)
}
}
用法:
collectionView.scrollToLast()