collectionView cellForItemAt 未被调用 [英] collectionView cellForItemAt not being called

查看:28
本文介绍了collectionView cellForItemAt 未被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UITableView,其中每个原型单元格中都有一个 UICollectionView.这个集合视图应该是一个图像网格.我对 Swift 非常陌生,已经在谷歌上搜索了几个小时(并阅读了大量 StackOverflow 文章),但似乎无法找到这个问题的正确答案.

I've got a UITableView where each prototype cell has a UICollectionView in it. This collection view is supposed to be a grid of images. I'm very new to Swift, and have googled for hours (and read plenty of StackOverflow articles), and cannot seem to find the correct answer to this question.

为什么没有调用 collectionView cellForItemAt?

How come collectionView cellForItemAt is not being called?

我看到我的 collectionView numberOfItemsInSection 方法和 collectionView sizeForItemAt 方法被调用.我确保将我的 TableViewCell 分配为 collectionView 的委托和数据源.我在带有插座的 Interface Builder 和我的自定义 TableViewCell 的 awakeFromNib 方法中使用了

I see that my collectionView numberOfItemsInSection method, and collectionView sizeForItemAt methods get called. I've made sure to assign my TableViewCell as the delegate and dataSource of the collectionView. I did this both in the Interface Builder with outlets, and again in my custom TableViewCell's awakeFromNib method with

cardImagesCollection.delegate = self
cardImagesCollection.dataSource = self

无论我做什么,我似乎都无法称之为.我确保我设置了良好的约束,因为这在 tableViewCells 上烧了我一次.

No matter what I do, I cannot seem to get this to call it. I made sure that I had good constraints set up because that burned me once on the tableViewCells.

对于我的一生,我无法让它加载 collectionView.

For the life of me, I cannot get it to load up the collectionView.

任何帮助将不胜感激.

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

Specifically, I'm using Xcode 9.4.1 and Swift 4.1.

谢谢

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
}

最新的CardsTableViewCell: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)
}

最新CardImageCell:UICollectionViewCell

@IBOutlet weak var imageView: UIImageView!

编辑 2:我尝试过的建议总结

人们建议在我构建 CollectionView 的 cellForRowAt 调用中为 CollectionView 设置数据源和委托:

Edit 2: Summary of suggestions I've tried

People have suggested setting the dataSource and delegate for the CollectionView inside the cellForRowAt call where I build the 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 调用中添加以下内容:

This did not change anything. They also suggested adding the following inside of that cellForRowAt call right after setting cell.card:

cell.cardImagesCollection.reloadData()

这也没有改变任何东西.

This also did not change anything.

我已经在我的 TableViewCell 的自定义类中放置了断点和日志记录,我可以看到 numberOfItemsInSection 为每个 TableViewCell 调用一次,并且它返回正确的数字(这个数字因 TableViewCell 而异).然后我还看到 sizeForItemAt 被调用 numberOfItemsInSection 返回的确切次数.但是,我没有看到 cellForItemAt 方法被调用过.

I've put breakpoints and logging in my TableViewCell's custom class and I can see that numberOfItemsInSection gets calls once per TableViewCell, and it returns the correct number (this number varies between TableViewCell). I then also see sizeForItemAt get called the exact amount of times that numberOfItemsInSection returns. I do not, however, see the cellForItemAt method get called ever.

这是日志:

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,我一开始就是这样做的.我还使用以下协议设置了自定义 TableViewCell,UICollectionViewDataSource、UICollectionViewDelegate、UICollectionViewDelegateFlowLayout 并且它不会抱怨缺少 cellForItemAt 方法.我目前在 InterfaceBuilder 中设置了委托和数据源,这就是我看到的方法调用的原因.

Some have suggested making sure to define cellForItemAt by using autocomplete as opposed to copying and pasting, which is how I did it in the first place. I also have my custom TableViewCell set up with the following protocols UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout and it does not complain about a lack of cellForItemAt method. I currently have the delegate and dataSource set in the InterfaceBuilder and that is what is giving me the method calls I'm seeing.

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

I've tried calling reloadData() on the collectionView in multiple places (the second I set the cell.card in my cellForRowAt, in my didSet call, and even the awakeFromNib method in the custom TableViewCell. Nothing works.

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

Other aspects of the TableCell render, but not this image collection. I've hardcoded a size for the CollectionView cells in the InterfaceBuilder, and even overrode sizeForItemAt to always return CGSize(width: 150, height: 150), just to be sure that the issue isn't having a cell that's too small to be visible. The tableView cell is set to auto height with latestCardsTableView.rowHeight = UITableViewAutomaticDimension, and I've added long text to my labels to ensure that they do grow in size with the text automatically.

有人有其他想法吗?有什么我可以从 InterfaceBuilder 截图的东西,或者我可以写一些 Swift 代码来强制一些 AutoLayout 的东西吗?我对任何事情都持开放态度.

Does anyone have any other ideas? Are there things I can screenshot from the InterfaceBuilder, or some Swift code I can write to force some AutoLayout stuff? I'm open to anything.

我发现如果我给我的 UITableViewCell 一个显式的大高度,UICollectionView 确实会显示并调用 cellForItemAt.那么,鉴于我在 TableView 上设置了 rowHeight = UITableViewAutomaticDimension,我该如何设置约束,以便 UICollectionView 强制 UITableViewCell 变大?

I've found that if I give my UITableViewCell an explicit large height, the UICollectionView does show up and does call cellForItemAt. So, how do I set my constraints so that the UICollectionView forces the UITableViewCell larger, given that I've got rowHeight = UITableViewAutomaticDimension set on my TableView?

推荐答案

也许你可以这样做,在您的 LatexCardsTableViewCell 类中编写一个方法,

Maybe you can do following, Write a method in your LatestCardsTableViewCell class,

func configureCell() {
      cardImagesCollection.reloadData()
}

并在 HomeViewController 的 cellForRowAtIndexPath 中调用此方法,如下所示,

and call this method, in HomeViewController's cellForRowAtIndexPath like following,

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

}

这篇关于collectionView cellForItemAt 未被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆