ios - UITableView 和 UICollectionView 在一行中重用第一行中的集合

标签 ios swift uitableview uicollectionview uicollectionviewcell

我有一个 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/

相关文章:

ios - 点击通知时出现弹出窗口问题(Deeplink)

ios - UIBezierPath 为每个部分放置一种颜色,导致最后一个颜色

ios - UITableView 静态单元格作为 subview ?

ios - 什么对我的 viewController 有很强的引用意义?

ios - 如何创建带下划线的 UITextField

javascript - 即使在添加控制台插件后,console.log 在 iOS Phonegap App 中也不起作用

iphone - UITableView 部分标题字体问题

ios - 在 Swift 中使用 .Success 枚举返回非可选

ios - 如何以 bgra8Unorm 像素格式绘制 MTLTexture

ios - UITableView reloadData异步