【问题标题】:Why is my collection view empty when I should have a bunch of reusable cells?当我应该有一堆可重复使用的单元格时,为什么我的集合视图是空的?
【发布时间】:2020-09-23 14:37:03
【问题描述】:

我有以下代码:

class FinalImageViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
    
    var not: [UIImage?] = [#imageLiteral(resourceName: "silly5"), #imageLiteral(resourceName: "special16")]
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 3
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return not.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CVImageView", for: indexPath) as! CVCell
        cell.cellImageView.image = not[indexPath.row]
        return cell
    }
    
    
    @IBOutlet weak var collectionView: UICollectionView!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        print(ImagesOnClick)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(CVCell.self, forCellWithReuseIdentifier: "CVImageView")
    }
    
}

还有我的牢房:

class CVCell: UICollectionViewCell {
    @IBOutlet weak var cellImageView: UIImageView!
    
}

然后在我的故事板中,我将 imageView 标记设置为 3,将单元格标识符设置为 CVImageView,但是当我运行项目时,集合视图全是白色,并且没有显示任何项目。顺便说一句,如果您想知道 ImagesOnClick 是什么,它只是用户选择的图像的数组。

【问题讨论】:

  • 可能需要加:func numberOfSections(in collectionView: UICollectionView) -> Int { return 1 }
  • 我只是尝试添加它并没有工作:(
  • ImagesOnClick 在哪里填充?顺便说一句,viewWithTag 已经过时了很长时间。设计一个带有插座的自定义单元(UICollectionViewCell 子类)。
  • 如果您查看我的问题,我已经通过创建 UICollectionViewCell class 更改了代码我也更改了 ImageOnClick not(不是两个图像文字的数组),仅用于测试目的但仍然得到相同的结果。

标签: swift xcode uicollectionview uicollectionviewcell


【解决方案1】:

你可以在故事板中找到你的班级名称,它应该是这样的

class FinalImageViewController: UIViewController,UICollectionViewDelegate, UICollectionViewDataSource {

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return ImagesOnClick.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withIdentifier: "CVImageView", for: indexPath) as! CVImageCell //replace CVImageCell with your cell class name
    cell.imageView.image = ImagesOnClick[indexPath.row] //replace imageView with your image view name with tag=3
    return cell
}


@IBOutlet weak var collectionView: UICollectionView!


override func viewDidLoad() {
    super.viewDidLoad()
    print(ImagesOnClick)
    collectionView.register(CVImageCell.self, forCellWithReuseIdentifier: "CVImageView") //replace CVImageCell with your cell class name
    collectionView.delegate = self
    collectionView.dataSource = self
}

}

【讨论】:

  • 这没有做任何事情,除非我应该在 Cell View Class 中添加一些内容
  • 我刚刚在上面的答案上传了一张图片,请看一下。我希望这能解决您的问题。
  • 请在我的问题下查看我最近的评论
  • 尝试为您的单元格设置背景颜色,例如: cell.backgroundColor = .red 如果之后您可以看到任何内容,这意味着您的图像视图数组可能有问题
  • 我修好了!我准备好了
猜你喜欢
  • 2015-11-30
  • 1970-01-01
  • 2019-03-14
  • 2017-01-28
  • 1970-01-01
  • 2020-03-04
  • 2019-09-03
  • 1970-01-01
  • 2013-05-21
相关资源
最近更新 更多