collectionView cellForItemAt未被调用

时间:2018-07-12 05:38:15

标签: ios swift xcode uicollectionview uicollectionviewcell

我有一个UITableView,每个原型单元中都有一个UICollectionView。该集合视图应该是图像网格。我是Swift的新手,已经在Google上搜索了几个小时(并阅读了很多StackOverflow文章),而且似乎无法找到该问题的正确答案。

为什么不调用collectionView cellForItemAt?

我看到我的collectionView numberOfItemsInSection方法和collectionView sizeForItemAt方法被调用。我已经确保将TableViewCell分配为collectionView的委托和dataSource。我既在带插座的界面生成器中进行此操作,又在自定义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,这是我首先要做的。我还使用以下协议UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout设置了自定义TableViewCell,它没有抱怨缺少cellForItemAt方法。我目前在InterfaceBuilder中设置了委托和数据源,这就是我所看到的方法调用的原因。

我尝试在多个位置上对collectionView调用reloadData()(第二个步骤是在cell.cardcellForRowAt调用中设置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更大?

Collection View Constriants

1 个答案:

答案 0 :(得分:0)

也许您可以执行以下操作, 在您的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

}

相关问题