【问题标题】:UITableviewcell not displaying UICollectionViewUITableviewcell 不显示 UICollectionView
【发布时间】:2018-04-13 21:46:43
【问题描述】:

我正在使用一个与 tableview 连接的 ViewController,并使用 ExpandableCell lib 来展开 UITableviewcell。

我在情节提要中使用 UICollectionview 创建了一个 expandcell,但是当我使用 dequeueReusableCell 时,他不会加载 UICollectionview

我正在使用 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 创建的

更新:故事板截图

【问题讨论】:

  • 您是否为您的集合视图实例设置了委托和数据源?如果您可以将故事板的屏幕截图与插座连接一起包括在内,这也可能会有所帮助。
  • @Pranay 我用截图更新问题,我可以在哪里设置委托和数据源?

标签: ios swift uitableview uicollectionview


【解决方案1】:

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

在这张图片中突出显示它们没有连接。

在您的 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 中,您可能会遇到一些编译器错误。希望这会有所帮助。

【讨论】:

  • 非常感谢!工作正常,但我唯一改变的是 func configure(withConciergeData concierge:Concierge,因为没有它它可以正常运行:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-18
  • 1970-01-01
  • 1970-01-01
  • 2014-12-27
  • 1970-01-01
  • 2019-11-16
  • 1970-01-01
相关资源
最近更新 更多