collectionView cellForItemAt 未被调用

问题描述 投票:0回答:1

我有一个

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

为什么collectionView cellForItemAt没有被调用?

我看到我的 collectionView

numberOfItemsInSection
方法和 collectionView
sizeForItemAt
方法被调用。我已确保将我的
TableViewCell
指定为 collectionView 的委托和数据源。我在带有插座的 Interface Builder 中执行了此操作,并再次在我的自定义 TableViewCell 的
awakeFromNib
方法中使用

执行了此操作
cardImagesCollection.delegate = self
cardImagesCollection.dataSource = self

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

在我的一生中,我无法让它加载collectionView。

任何帮助将不胜感激。

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

谢谢

编辑 - 添加示例代码

HomeViewController: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:我尝试过的建议总结

人们建议在我构建 CollectionView 的

cellForRowAt
调用中设置 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
,而不是复制和粘贴,这就是我最初的做法。我还使用以下协议设置了自定义 TableViewCell
UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout
,并且它不会抱怨缺少
cellForItemAt
方法。我目前在 InterfaceBuilder 中设置了委托和数据源,这就是我所看到的方法调用的原因。

我尝试在多个地方调用collectionView上的

reloadData()
(第二次我在我的
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
。那么,鉴于我在 TableView 上设置了
rowHeight = UITableViewAutomaticDimension
,我该如何设置约束,以便 UICollectionView 强制 UITableViewCell 变大?

ios swift xcode uicollectionview uicollectionviewcell
1个回答
2
投票

也许你可以做以下事情, 在您的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
}
© www.soinside.com 2019 - 2024. All rights reserved.