在 Swift 中获取 UIcollectionView 中的下一个单元格
Get next cell in UIcollectionView in Swift
我需要获取集合视图中 cellForItem 内的下一个单元格,以便更新视图对象。当我在下面尝试以下操作时,它不起作用。我也试过 indexPathForVisibleItems
传入 indexPath.row + 1
并产生索引超出范围错误。
let index = IndexPath(row: indexPath.row + 1, section: indexPath.section)
if let nextCell = collectionView.cellForItem(at: index) as! MKRCell {
nextCell.setupWaitView(time: timeToWait)
nextCell.waitViewHeightConstraint.constant = 80
nextCell.waitView.alpha = 1
nextCell.waitView.isHidden = false
}
这有可能实现还是我需要通过其他方式实现?
谢谢
不行,在cellForItemAt
初始化之前是不可能拿到cell对象的,但是这里
您可以在显示来自 UICollectionViewDelegate
的单元格之前接听电话
func collectionView(_ collectionView: UICollectionView,willDisplay cell: UICollectionViewCell,forItemAt indexPath: IndexPath) {
if let myCell = cell as? MKRCell {
}
}
AND
如果要设置单元格,则必须在 UICollectionViewDataSource
中设置视图
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
}
您应该更新单元格:
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
请记住只修改您将从该方法返回的单元格。那时其他单元格可能不存在。
或者,您可以保留对单元格的弱引用并在需要时更新它。
我需要获取集合视图中 cellForItem 内的下一个单元格,以便更新视图对象。当我在下面尝试以下操作时,它不起作用。我也试过 indexPathForVisibleItems
传入 indexPath.row + 1
并产生索引超出范围错误。
let index = IndexPath(row: indexPath.row + 1, section: indexPath.section)
if let nextCell = collectionView.cellForItem(at: index) as! MKRCell {
nextCell.setupWaitView(time: timeToWait)
nextCell.waitViewHeightConstraint.constant = 80
nextCell.waitView.alpha = 1
nextCell.waitView.isHidden = false
}
这有可能实现还是我需要通过其他方式实现?
谢谢
不行,在cellForItemAt
初始化之前是不可能拿到cell对象的,但是这里
您可以在显示来自 UICollectionViewDelegate
func collectionView(_ collectionView: UICollectionView,willDisplay cell: UICollectionViewCell,forItemAt indexPath: IndexPath) {
if let myCell = cell as? MKRCell {
}
}
AND
如果要设置单元格,则必须在 UICollectionViewDataSource
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
}
您应该更新单元格:
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
请记住只修改您将从该方法返回的单元格。那时其他单元格可能不存在。
或者,您可以保留对单元格的弱引用并在需要时更新它。