ios - collectionView cellForItemAt 没有被调用

标签 ios swift xcode uicollectionview uicollectionviewcell

我有一个 UITableView其中每个原型(prototype)单元都有一个 UICollectionView在里面。这个 Collection View 应该是一个图像网格。我对 Swift 很陌生,已经搜索了几个小时(并阅读了大量 StackOverflow 文章),但似乎无法找到这个问题的正确答案。

为什么没有调用collectionView cellForItemAt?

我看到我的 collectionView numberOfItemsInSection方法和collectionView sizeForItemAt方法被调用。我已确定分配我的 TableViewCell作为 collectionView 的委托(delegate)和数据源。我在带有 socket 的 Interface Builder 中以及在我的自定义 TableViewCell 的 awakeFromNib 中都这样做了。方法与

cardImagesCollection.delegate = self
cardImagesCollection.dataSource = self

无论我做什么,我似乎都无法得到这个称呼。我确保我设置了良好的约束,因为这让我在 tableViewCells 上烧了一次。

对于我的生活,我无法让它加载collectionView。

任何帮助将不胜感激。

具体来说,我使用的是 Xcode 9.4.1 和 Swift 4.1。

谢谢

编辑 - 添加示例代码

主 View Controller :UIViewController
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "LatestCardCell", for: indexPath) as! LatestCardsTableViewCell

    cell.backgroundColor = UIColor.clear
    cell.card = cards[indexPath.row]
    return cell
}

LatestCardsTableViewCell: UITableViewCell
@IBOutlet weak var cardImagesCollection: UICollectionView!

var card: Card! {
    didSet {
        titleLabel.attributedText = FontUtil.attributedCardTitleText(string: card.title)
        descriptionLabel.attributedText = FontUtil.attributedCardDescriptionText(string: card.description)
    }
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    print("count")
    return card.imageUrls.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    print("here")
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "LatestCardsImage", for: indexPath as IndexPath) as! LatestCardImageCell

    //let url = card.imageUrls[indexPath.row]
    //cell.imageView.image = UIImage(named: "test")

    cell.backgroundColor = UIColor.blue
    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: 150, height: 150)
}

LatestCardImageCell:UICollectionViewCell
@IBOutlet weak var imageView: UIImageView!

编辑2:我尝试过的建议摘要

人们建议在 cellForRowAt 中为 CollectionView 设置数据源和委托(delegate)。调用我构建 CollectionView 的位置:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "LatestCardCell", for: indexPath) as! LatestCardsTableViewCell

    cell.cardImagesCollection.delegate = cell
    cell.cardImagesCollection.dataSource = cell
    cell.backgroundColor = UIColor.clear
    cell.card = cards[indexPath.row]
    return cell
}

这并没有改变什么。他们还建议在设置 cell.card 后立即在该 cellForRowAt 调用中添加以下内容:
cell.cardImagesCollection.reloadData()

这也没有改变什么。

我在 TableViewCell 的自定义类中设置了断点并登录,我可以看到 numberOfItemsInSection每个 TableViewCell 调用一次,它返回正确的数字(这个数字因 TableViewCell 而异)。然后我还看到sizeForItemAt被调用的确切次数 numberOfItemsInSection返回。但是,我没有看到 cellForItemAt方法永远被调用。

这是日志记录:
numberOfItemsInSection: 6
sizeForItemAt: Section 0 Row 0
sizeForItemAt: Section 0 Row 1
sizeForItemAt: Section 0 Row 2
sizeForItemAt: Section 0 Row 3
sizeForItemAt: Section 0 Row 4
sizeForItemAt: Section 0 Row 5
numberOfItemsInSection: 2
sizeForItemAt: Section 0 Row 0
sizeForItemAt: Section 0 Row 1
numberOfItemsInSection: 4
sizeForItemAt: Section 0 Row 0
sizeForItemAt: Section 0 Row 1
sizeForItemAt: Section 0 Row 2
sizeForItemAt: Section 0 Row 3

有些人建议确保定义 cellForItemAt通过使用自动完成而不是复制和粘贴,我一开始就是这样做的。我还使用以下协议(protocol)设置了自定义 TableViewCell UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout它不会提示缺少 cellForItemAt方法。我目前在 InterfaceBuilder 中设置了委托(delegate)和数据源,这就是我所看到的方法调用。

我试过调用reloadData()在多个地方的 collectionView 上(第二个我在我的 cell.card 中设置了 cellForRowAt ,在我的 didSet 调用中,甚至在自定义 TableViewCell 中设置了 awakeFromNib 方法。没有任何效果。

TableCell 的其他方面呈现,但不是这个图像集合。我在 InterfaceBuilder 中硬编码了 CollectionView 单元格的大小,甚至覆盖了 sizeForItemAt 以始终​​返回 CGSize(width: 150, height: 150) ,只是为了确保问题不在于单元格太小而无法看到。 tableView 单元格设置为自动高度,latestCardsTableView.rowHeight = UITableViewAutomaticDimension ,并且我在标签中添加了长文本,以确保它们的大小随着文本自动增长。

有没有人有任何其他想法?有没有我可以从 InterfaceBuilder 截屏的东西,或者我可以编写一些 Swift 代码来强制执行一些 AutoLayout 的东西?我对任何事情都持开放态度。

编辑3:一线希望

我发现如果我给我的 UITableViewCell 一个显式的大高度,UICollectionView 会显示并调用 cellForItemAt .那么,我如何设置我的约束,以便 UICollectionView 强制 UITableViewCell 更大,因为我有 rowHeight = UITableViewAutomaticDimension在我的 TableView 上设置?

Collection View Constriants

最佳答案

也许你可以做以下,
在您的 LatestCardsTableViewCell 类中编写一个方法,

func configureCell() {
      cardImagesCollection.reloadData()
}

并在 HomeViewController 的 cellForRowAtIndexPath 中调用此方法,如下所示,
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "LatestCardCell", for: indexPath) as! LatestCardsTableViewCell

cell.backgroundColor = UIColor.clear
cell.card = cards[indexPath.row]
cell. configureCell()
return cell

}

关于ios - collectionView cellForItemAt 没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51298015/

相关文章:

iphone - 可以在 iTunes Connect 中的应用程序描述中添加制表符空间吗?

ios - 多个 UIImage View

json - 使用 json 数据的 Food 类的正确结构

swift - 将 ForEach 与字符串数组一起使用 - [String] 没有成员 'identified'

ios - Swift:自定义 UIView 未根据约束调整大小

swift - 谷歌地图当前位置 swift

c++ - 我怎样才能忽略 Xcode 中的一些错误?

ios - 运行调试器时如何阻止Xcode 6显示反汇编

ios - 将 AppDelegate.h 和 AppDelegate.m 替换为 AppDelegate.swift

ios - UITextView 接收文本形式的听写,但文本在 textView.text 属性中为 nil