无法滚动到底部 UICollectionview
Can' t scroll to bottom UICollectionview
我无法滚动到底部,我尝试了其他解决方案但没有成功。
我正在使用 IGLiskit
这是错误:
attempt to scroll to invalid index path
代码如下:
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)
}
}
}
}
您在 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()
我无法滚动到底部,我尝试了其他解决方案但没有成功。
我正在使用 IGLiskit
这是错误:
attempt to scroll to invalid index path
代码如下:
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)
}
}
}
}
您在 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()