ios - UITableviewcell 不显示 UICollectionView

标签 ios swift uitableview uicollectionview

我正在使用与 TableView 连接的 ViewController 并使用 ExpandableCell lib 来展开 UITableviewcell。

我在 Storyboard中使用 UICollectionview 创建了一个扩展单元,但是当我使用 dequeueReusableCell 时,他不会加载 UICollectionview

Cell display

我使用的是 Swift 4 Xcode 9.2

这是我在 View Controller 的代码

 override func viewDidLoad() {
        super.viewDidLoad()

 self.tableView.register(ServicesCell.self, forCellReuseIdentifier: "servicesCell")
        self.tableView.register(ConciergeCell.self, forCellReuseIdentifier: "conciergeCollection")

}

extension ViewController : ExpandableDelegate {
 func expandableTableView(_ expandableTableView: ExpandableTableView, expandedCellsForRowAt indexPath: IndexPath) -> [UITableViewCell]? {

                let cell = tableView.dequeueReusableCell(withIdentifier: ConciergeCell.ID) as! ConciergeCell



                return [cell]

}

extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return concierge.count


    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        switch indexPath.section {


                cell.displayContent(image: concierge[indexPath.row].image!, title: concierge[indexPath.row].name!)
                cell.backgroundColor = UIColor.dark70
                cell.layoutSubviews()




        return cell
    }



    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("Collection view at row \(collectionView.tag) selected index path \(indexPath)")
    }

UITableviewcell 类

class ConciergeCell: UITableViewCell {
    static let ID = "conciergeCollection"

    @IBOutlet weak var collectionView: UICollectionView!


}

UICollectionviewcell

class ConciergeCollectionCell: UICollectionViewCell {

    @IBOutlet var mainImage: UIImageView!
    @IBOutlet var mainLabel: UILabel!



    func displayContent(image: UIImage, title: String) {
        mainImage.image = image
        mainLabel.text = title
    }

}

我可以采取哪些步骤来解决这个问题?我不知道是否有必要在 viewDidLoad 中注册 uitableviewcell 因为我是使用 Storyboard 创建的

更新: Storyboard的屏幕截图 enter image description here

enter image description here

enter image description here

最佳答案

问题是,您没有为 UICollectionView 实例设置委托(delegate)和数据源。

此图中突出显示的部分表明它们未连接。

enter image description here

在您的 ConciergeCell 类中,进行以下更改:

class ConciergeCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource {
    static let ID = "conciergeCollection"

    @IBOutlet weak var collectionView: UICollectionView!
    private var concierge: Concierge

    func awakeFromNib() {
        super.awakeFromNib()

        self.collectionView.delegate = self
        self.collectionView.dataSource = self
    }

    func configure(withConciergeData concierge:Concierge) {
        self.concierge = concierge
        self.concierge.reloadData()
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.concierge.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        cell.displayContent(image: self.concierge[indexPath.row].image!, title: concierge[indexPath.row].name!)
        cell.backgroundColor = UIColor.dark70
        cell.layoutSubviews()
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("Collection view at row \(collectionView.tag) selected index path \(indexPath)")
    }
}

然后,在以下函数中,在返回 [cell] 数组之前,使用礼宾数据的实例调用 configure(withConciergeData:) 函数,如下所示所以:

extension ViewController : ExpandableDelegate {
    func expandableTableView(_ expandableTableView: ExpandableTableView, expandedCellsForRowAt indexPath: IndexPath) -> [UITableViewCell]? {
        let cell = tableView.dequeueReusableCell(withIdentifier: ConciergeCell.ID) as! ConciergeCell
        cell.configure(withConciergeData: concierge)
        return [cell]
    }
}

请注意,我在这里输入了这段代码。如果将其复制并粘贴到 Xcode 中,可能会出现一些编译器错误。希望这会有所帮助。

关于ios - UITableviewcell 不显示 UICollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49825536/

相关文章:

ios - Xcode 6 Beta 5 If 语句不工作

ios - UITableViewCell 下边距约束

ios - UITableViewCell 编辑模式中的左圆出现在 iOS8

ios - SwiftUI - 检查图像是否存在

iphone - 我应该在 viewDidUnload 和 dealloc 中注销 NSNotification 吗?

ios - UIImage 到 CVPixelBuffer 内存问题

uitableview - 如何最初在UITableView中选择一行

ios - 大小为零的 UIView 对象是否占用大量内存?

ios - 我如何将其转换为 Swift?

ios - 如何在 XCUI 测试自动化框架中组织和分组测试用例