【发布时间】:2018-09-17 01:45:29
【问题描述】:
我正在 UITableView 的每个单元格内实现 UICollectionView 现在我从 API 获取数据并将这些数据保存在数组中,问题是 UICollectionView 委托方法 numberOfItemsInSection 在数组填充数据之前当然会被调用,所以我得到 Index out of range 错误。 问题是如何为返回的计数提供默认值或如何改变数组。 这是我正在使用的代码:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// let cell = collectionView.superview as! MainTableViewCell
// let index = tableView.indexPath(for: cell)
let countIndex = self.consultations[collectionView.tag].images.count
return countIndex
}
【问题讨论】:
-
您可能遇到了重复使用单元格的问题。在提供数据之前是否调用
numberOfItems无关紧要;您应该在重新使用时清除阵列。self.consultations应始终处于连贯状态。此外,依赖tags 是脆弱的。您的单元格应该只有一个images数组;它不需要知道任何封闭数组。
标签: ios arrays swift uitableview uicollectionview