未调用表视图单元格内的集合视图

时间:2019-06-01 15:44:05

标签: ios swift uitableview uicollectionview

我正在尝试在表格视图单元格内实现集合视图。

我的表格视图单元格是xib,并且已将集合视图拖到其中。

然后,我为集合视图单元格创建了一个类和xib:

MASK = A.sum(axis=0) < 28

我给xib分配了“ MyCollectionViewCell”类,并为其赋予了标识符“ MyCollectionViewCell”。

然后,在我的表格视图单元格类中,执行以下操作:

class MyCollectionViewCell: UICollectionViewCell {

    var media: Image?

    @IBOutlet weak var imageView: UIImageView!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    func initialize(media: PostImage) {
        self.media = media

        if let url = media.url {
            imageView.kf.setImage(with: URL(string: url))
        }
    }

}

问题是,运行此视图时,集合视图从不显示。标题标签文本显示正常,但未显示收藏夹视图,并且我不知道自己在做什么错。

class MyTableViewCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource { var post: Post! @IBOutlet weak var title: UILabel! @IBOutlet weak var mediaCollectionView: UICollectionView! override func awakeFromNib() { super.awakeFromNib() mediaCollectionView.delegate = self mediaCollectionView.dataSource = self let mediaCollectionViewCell = UINib(nibName: "MyCollectionViewCell", bundle: nil) mediaCollectionView.register(mediaCollectionViewCell, forCellWithReuseIdentifier: "MyCollectionViewCell") } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 2 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCollectionViewCell", for: indexPath as IndexPath) as? MyCollectionViewCell else { fatalError("The dequeued cell is not an instance of MyCollectionViewCell.") } let media = post.images[indexPath.row] cell.initialize(media: media) return cell } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state } func initialize(post: Post) { self.post = post title.text = post.title self.mediaCollectionView.reloadData() } } 似乎没有被调用,因为当我在函数顶部添加cellForItemAt时,它永远不会显示在控制台中。

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

我认为问题在于集合视图的高度很小,因此未显示。 尝试设置表格视图单元格的高度:

func tableView(_: UITableView, heightForRowAt _: IndexPath) -> CGFloat {
     return 100
}

其中100应该大于集合视图

相关问题