【问题标题】:Collection View Cells Not Showing集合视图单元格未显示
【发布时间】:2016-09-12 17:51:49
【问题描述】:

我知道有很多问题可以回答这个问题,但我的情况略有不同。如果您使用情节提要,大多数答案都告诉我删除这行代码:

collectionView?.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: cellId)

但我不能这样做,因为我同时使用故事板和非故事板。

我从一个程序化的推送视图控制器(故事板控制器-标签栏控制器)转换到这个集合视图控制器(非故事板视图),如下所示:

func showChatLogController(user: User) {
    let chatLogController = ChatLogController(collectionViewLayout: UICollectionViewLayout())
    chatLogController.user = user
    chatLogController.hidesBottomBarWhenPushed = true
    navigationController?.pushViewController(chatLogController, animated: true)

}

我的集合视图代码似乎是写的,我不知道问题是什么。如果您需要在此处查看集合视图代码,则为:

let cellId = "cellId"

override func viewDidLoad() {
    super.viewDidLoad()

    collectionView?.backgroundColor = UIColor.whiteColor()
    collectionView?.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: cellId)

    setUpInputComponents()

    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(SignUpViewController.dismissKeyboard))
    view.addGestureRecognizer(tap)
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 5
}

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellId, forIndexPath: indexPath)

    cell.backgroundColor = UIColor.blueColor()

    return cell
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    return CGSize(width: view.frame.height, height: 80)
} 

由于某种原因,它不显示单元格。我不知道为什么。任何帮助将不胜感激。

PS:我不知道这是否重要,但要进入集合视图控制器,我来自标签栏控制器。

【问题讨论】:

  • 有没有人对我很奇怪的情况有什么建议
  • 嗨,Rohan,您解决了这个问题吗?我们似乎发生了完全相同的事情。我还注意到,如果我开始滚动单元格应显示的位置,它们会突然出现。

标签: ios swift uicollectionviewcell collectionview


【解决方案1】:

在您的viewDidload 赞中将您的收藏视图的datasourcedelegate 设置为self,

  yourCollectionView.dataSource = self
  yourCollectionView.delegate = self

【讨论】:

  • 有没有人对我很奇怪的情况有什么建议
【解决方案2】:
  • 是否调用了 cellForRowAtIndexPath?如果您在其中打印某些内容,它会被记录吗?
  • 您的收藏视图是否显示在您期望的位置?如果你设置它的背景颜色,你看到了吗?
  • 在 sizeForItemAtIndexPath 中打印您返回的尺寸。是你所期望的还是 0 潜入其中?

【讨论】:

  • 它无法识别我的 cellForItemAtIndexPath 或 sizeForItemAtIndexPath。我不知道为什么它正在阅读这些行: numberOfItemsInSection 和 collectionView?.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: cellId)
  • 什么时候设置collection view的数据源?我想知道在注册单元以供重用之前,集合视图是否会尝试重新加载数据。如果在 registerClass 之后调用 reloadData() 会发生什么?
  • 有没有人对我很奇怪的情况有什么想法
【解决方案3】:

我最近遇到了这个问题,发现我错误地实例化了 Collection View 控制器。我之前将其实例化为:

let CollectionViewController = CollectionViewController(collectionViewLayout: UICollectionViewLayout())

什么时候应该:

let CollectionViewController = CollectionViewController(collectionViewLayout: UICollectionViewFlowLayout())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-26
    • 1970-01-01
    相关资源
    最近更新 更多