我有一个 Collection View ,代表工作人员的每周计划。 当我初始化集合时,我将所有工作槽设置为 TRUE,这意味着所有集合项都应填充为工作槽。
我现在正在尝试实现一个数据模型,它是一个表示每周计划的 bool 数组,并将传递给 Collection View 。 我似乎无法找到一种方法来迭代所有单元格并更新它们。当我使用 visibleCells() 时,我得到一个空数组。
一些代码:
func updateShifts(schedule: WeeklySchedule, shift: Int){
print(self.collectionView!.visibleCells.count) // prints 0
for cell in self.collectionView!.visibleCells as! [SchedulePickupCell] {
cell.setWorking(isWorkingInput: false)
}
}
在代码中,SchedulepickupCell 是我自定义的 Collection View 单元格。 此代码位于我的类中:
class ScheduleCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout
现在我不确定可见单元格意味着什么,我经常让所有项目在我的应用程序中可见,我只想迭代并将它们的 isHidden 属性更改为 true。
最佳答案
虽然 self.collectionView!.visibleCells.count
应该包含所有可见单元格,但我相信您应该采用不同的方法。
确保在 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
中根据模型设置单元格,然后:
- 更新您的模型
- 调用
collectionView.reloadData()
关于swift - 遍历 Collection View 中的所有单元格,可见与否,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42053267/