0

我需要在集合视图中的 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
            }

这可能实现还是我需要通过其他方式来实现?

谢谢

4

2 回答 2

2

不,在初始化之前无法获取单元格对象,cellForItemAt但在这里您可以在显示单元格之前收到调用UICollectionViewDelegate

func collectionView(_ collectionView: UICollectionView,willDisplay cell: UICollectionViewCell,forItemAt indexPath: IndexPath) {
     if let myCell = cell as? MKRCell {

     }
}

如果要设置单元格,则必须在UICollectionViewDataSource

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

}
于 2017-10-24T09:41:35.340 回答
1

您应该更新单元格:

func collectionView(_ collectionView: UICollectionView, 
    cellForItemAt indexPath: IndexPath) -> UICollectionViewCell

请记住只修改您将从该方法返回的单元格。那时其他细胞可能不存在。

或者,您可以保留对单元格的弱引用并在需要时对其进行更新。

于 2017-10-24T09:42:02.193 回答