UICollectionViewCells 未正确显示内容

时间:2021-01-15 16:12:43

标签: ios swift uicollectionviewcell

我有两个 ViewControllers,每个都有一个 collectionView (我们将其命名为 CV)

第一个 CV Cell (名为 MainPageCell) 是应用程序的主要结构,在它的其他组件中,它显示一个名为 imageViewGrid 的 UIView。

第二个控制器的目的只是将它的CV作为子视图添加到自己,然后,它可以添加为第一个控制器的孩子,然后插入它的视图在 imageViewGrid 上

简而言之,我正在尝试在另一个简历组件中显示简历。但是第二个简历的单元格没有加载,或者如果加载了,滚动时它们会消失。只有一个单元格可以显示内容,直到我滚动。

这个 GIF 描述了我的情况:Click here

第一份简历 cellForItemAt:

func collectionView(_ collectionView: UICollectionView,
                    cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: mainReuseIdentifier, for: indexPath) as! MainPageCell
        
    displayController(contentController: photoGridController, on: cell.imageViewGrid)
    
    return cell
}

在 MainPageCell 中的 imageViewGrid 声明:

    var imageViewGrid: UIView = {
    let ivg = UIView()
    ivg.backgroundColor = Colors.lightBlue
    ivg.translatesAutoresizingMaskIntoConstraints = false
    return ivg
}()

显示控制器功能:

func displayController(contentController content: UIViewController, on view: UIView) {
    addChild(content)
    view.addSubview(content.view)
    content.view.frame = view.bounds
    content.didMove(toParent: self)
}

第二个简历 cellForItemAt:

    func collectionView(_ collectionView: UICollectionView,
                    cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: photoReuseIdentifier, for: indexPath) as! PhotoGridCell
    
    cell.backgroundColor = Colors.lightBlue
    cell.item = names[indexPath.item]
    
    return cell
}

第二个 CV 单元组件:

    var item: String? {
    didSet {
        guard let myItem = item,
              let image = UIImage(named: myItem) else { return }
        
        if image == UIImage(named: myItem) {
            gridImageView.image = image
        } else {
            gridImageView.image = UIImage(named: "fbicon")
        }
    }
}

let gridImageView: UIImageView = {
   let iv = UIImageView()
    iv.contentMode = .scaleAspectFill
    iv.translatesAutoresizingMaskIntoConstraints = false
    return iv
}()

我不知道我是否应该重新加载一些东西,或者我是否忘记了一个步骤,但还无法完成这项工作。希望得到一些帮助。

0 个答案:

没有答案