【问题标题】:Xcode Swift UICollectionView: cellForItemAt IndexPath not called but numberOfItemsInSection doesXcode Swift UICollectionView:cellForItemAt IndexPath 未调用但 numberOfItemsInSection 调用
【发布时间】:2021-08-19 09:33:04
【问题描述】:

我目前正在使用 UICollectionView 编写应用程序,仅当我启动应用程序时,CollectionView 不显示。在我的应用程序中,我不使用 StoryBoard,而只使用 ViewController。这就是为什么我在我的应用程序中从 UICollectionViewController 继承。但是当我启动应用程序时, UICollectionView 根本不显示。通过打印命令,我发现调用了 numberOfItemsInSection 方法,但没有调用 cellForItemAt indexPath。我也知道这可能是一个双重问题,但我仍然没有找到解决方案 2 小时。 CollectionView.delegate 和 collectionView.dataSource 都设置为 = self 并且约束实际上没有问题,因为例如我可以将 UICollectionView 的背景变为黑色,并且所有内容都在那里正确显示。 我希望有一个人可以帮助我。 最好的问候

// MARK: - Collectionview
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    print("Item wurde erstellt")
    return messages.count
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! ChatMessageCollectionViewCell
    
    let message = messages[indexPath.item]
    cell.textView.text = message.message
    
    setUpCell(cell, message)
    cell.bubbleViewWidthAnchor?.constant = estimateFrameForText(text: message.message!).width + 32
    
    print("Celle wird erstellt")
    
    return cell
}

【问题讨论】:

  • 你检查你继承UICollectionViewDataSource吗?
  • 嗨。谢谢您的回答!不,我不继承 UICollectionViewDataSource。但即使我尝试继承它,我也会收到错误:Redundant conformance of 'ChatController' to protocol 'UICollectionViewDataSource'

标签: swift xcode uicollectionview uicollectionviewcell


【解决方案1】:

我认为您错过了单元格的高度和宽度。您还应该检查消息数组计数,可能为零。

extension YouClass: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return messages.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! ChatMessageCollectionViewCell
    
    let message = messages[indexPath.item]
    cell.textView.text = message.message
    
    setUpCell(cell, message)
    cell.bubbleViewWidthAnchor?.constant = estimateFrameForText(text: message.message!).width + 32
    
    print("Celle wird erstellt")
    
    return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: collBanner.frame.width, height: 140)
    }
}

【讨论】:

  • 感谢您的回答。不幸的是,我已经指定了高度和宽度。此外,消息数组对我来说也不为空。我可以通过打印看到...此外,如果我只是使用 numberOfItemsInSection 方法返回例如 10,则不会调用 cellForItemAt indexPath 方法
猜你喜欢
  • 2021-07-04
  • 2015-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多