首页 > 解决方案 > swift部分中TableViewCell中的CollectionVIew

问题描述

我有 7 个部分的 TableViewController,每个部分有 1 行。

在第 7 节需要将 CollectionView 与 UIImageView 放在一起。

我不知道我做错了什么。我的代码如下。用户有 9 张图片,但没有显示。

class NewDetailPhotographerTableViewController: UITableViewController {
    // loading data for display in view
}

extension NewDetailPhotographerTableViewController {

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 7
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let photo = photographer {
            switch section {
            case 1:
                return photo.photoShootingStyle.isEmpty ? 0 : 1
            case 2:
                return photo.languages.isEmpty ? 0 : 1
            case 3:
                return photo.equipment.isEmpty ? 0 : 1
            case 5:
                return photo.biography.isEmpty ? 0 : 1
            case 6:
                return photo.images.isEmpty ? 0 : 1
            default:
                return 1
            }
        }
        return 0
    }

    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0
    }

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }

    override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
}

extension NewDetailPhotographerTableViewController: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return photographer?.images.count ?? 0
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imagesCollectionCell", for: indexPath) as! NewDetailPhotographerCollectionViewCell
        cell.imageView.kf.setImage(with: URL(string: photographer!.images[indexPath.item]))
        return cell
    }
}

class NewDetailPhotographerTableViewCell: UITableViewCell {

    @IBOutlet weak var collectionView: UICollectionView!

    override func prepareForReuse() {
        super.prepareForReuse()
        self.collectionView.reloadData()
    }
}

class NewDetailPhotographerCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var imageView: UIImageView!

    override func awakeFromNib() {
        super.awakeFromNib()
        imageView.contentMode = .scaleAspectFill
        imageView.layer.masksToBounds = true
    }
}

在屏幕截图中可见 6 个部分,因为第 7 个部分是不可见的。

包括故事板屏幕截图。

在此处输入图像描述

在此处输入图像描述

标签: swiftxcodeuicollectionviewtableview

解决方案


推荐阅读