【问题标题】:How to load a UITable with custom cell inside a UICollectionViewCell that is already a NIB如何在已经是 NIB 的 UICollectionViewCell 中加载带有自定义单元格的 UITable
【发布时间】:2017-10-07 14:08:36
【问题描述】:

我有一个 UICollection 视图,它有两个自定义单元格。 UICollectionViewCell 自定义单元格是使用 NIB 构建的。在其中一个 UICollectionViewCell NIB 中,我有一个 UITableView。但是,UITableView 本身具有自定义单元格。所以我的问题是:

如何为 UITableView 加载自定义单元格而不是 UICollectionViewCell NIB?

import UIKit

class HeaderFooterCollectionViewCell: UICollectionViewCell {

    //Tableview
    @IBOutlet weak var tableView: UITableView!

    //Buttons
    @IBOutlet weak var resetButton: UIButton!
    @IBOutlet weak var periodButton: UIButton!
    @IBOutlet weak var undoButton: UIButton!


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

        // Register cell classes
        tableView.register(UINib(nibName: "ShotInformationTableViewCell", bundle: nil), forCellReuseIdentifier: "cell")
    }

}

extension HeaderFooterCollectionViewCell: UITableViewDataSource {

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

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ShotInformationTableViewCell

        return cell
    }
}

【问题讨论】:

    标签: ios swift uitableview uicollectionview nib


    【解决方案1】:
    let cell =  Bundle.main.loadNibNamed("XIB_File_Name", owner: self, options: nil)?.first as! XIB_Class_Name
    

    在几乎所有情况下,XIB_File_Name == XIB_Class_Name

    【讨论】:

    • 嘿@PaulS。我在返回cellForItem 时使用我的答案,但不幸的是它崩溃了,即使使用let cell = UINib(nibName: "XIB_File_Name", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! XIB_Class_Name 结果是一样的,那么我的答案对你有什么用?
    • 这是我的代码let cell = Bundle.main.loadNibNamed("ShotInformationTableViewCell", owner: self, options: nil)?.first as! ShotInformationTableViewCell 工作正常,没有崩溃。
    • 对不起,我问的是收藏视图的情况!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-08
    相关资源
    最近更新 更多