我有一个 UITableView,每个单元格中都有一个 UICollectionView。
问题是表格重用了第一个单元格数据,因此它在最新的表格单元格中显示第一个集合...
UITableView 代码 -
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath) as! ProjectTableViewCell
if !didIndex{
cell.i = i
i += 1
if i >= projects.count{
i = 0
didIndex = true
}
}
return cell
}
以及 UITableViewCell 内的 UICollectionView -
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
photosAsset = (PHAsset.fetchAssets(in: assetCollections[i], options: nil) as AnyObject!) as! PHFetchResult<AnyObject>!
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath) as! ProjectsCollectionViewCell
let asset: PHAsset = self.photosAsset[indexPath.item] as! PHAsset
PHImageManager.default().requestImage(for: asset, targetSize: self.assetThumbnailSize, contentMode: .aspectFill, options: nil, resultHandler: {(result, info)in
if result != nil {
cell.imageView.image = result
}
})
return cell
}
我检查了代码,看起来UICollectionView中的结果,cellForItemAt是可以的,所以我假设问题是重复使用表格中的单元格,试图找到解决方案!
谢谢大家!
已解决 我使用了indexPath.section
最佳答案
您不应该为单元格分配索引 - 如果这样做非常脆弱。如果移动或删除单元格会发生什么?
看起来您正在使用 i
作为 assetCollections
中的索引,在这种情况下为什么不......
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath) as! ProjectTableViewCell
cell.assetCollection = assetCollections[indexPath.section]
return cell
}
和
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
photosAsset = (PHAsset.fetchAssets(in: assetCollection, options: nil) as AnyObject!) as! PHFetchResult<AnyObject>!
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath) as! ProjectsCollectionViewCell
let asset: PHAsset = self.photosAsset[indexPath.item] as! PHAsset
PHImageManager.default().requestImage(for: asset, targetSize: self.assetThumbnailSize, contentMode: .aspectFill, options: nil, resultHandler: {(result, info)in
if result != nil {
cell.imageView.image = result
}
})
return cell
}
关于ios - UITableView 和 UICollectionView 在一行中重用第一行中的集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51562032/