【问题标题】:Load UITableViewCell to UITableView in xib file将 UITableViewCell 加载到 xib 文件中的 UITableView
【发布时间】:2018-05-25 21:02:05
【问题描述】:

好的,我看过很多论坛,但没有找到任何具体的问题。

  • 我有一个 ViewController,我在其中添加了一个 UITableView。

  • UITableView 加载一个作为 xib 文件创建的 UITableViewCell

  • TableViewCell 有一个 UITableView,而 UITableView 又创建了一个 UITableViewCell 作为 xib 文件。

  • 这是如何在 Firts UITableView 中加载第一个 UITableViewCell。 ViewController 中的负载:

    tableDetails.delegate = self tableDetails.dataSource = self tableDetails.register(UITableViewCell.self, forCellReuseIdentifier: "DepartureDetailTableViewCell")

问题是我不知道如何从 ViewController 加载 xib 文件中的 Second UITableView 中的 Second UITableViewCell。有什么帮助吗?此外,如何为第二个 UITableView/UITableViewCell 执行此操作。

PD.:看这个教程,但他在 ViewController 中使用原型单元:https://www.youtube.com/watch?v=znGd5kyIdgM

请帮忙!

更新:

好的,我用@Abdelahad Darwish 解决方案解决了这个问题!但我还有另一个问题...在 DepartureDetailTableViewCell 中加载 DepartureInsideTableViewCell 时...不显示任何内容...它显示如下:

必须在两个视图“May 30, 2018 - 01:04”和“May 30, 07:47”中间显示 DepartureInsideTableViewCell

有什么帮助吗?

【问题讨论】:

  • 所以你有UIViewControllerTableViewMain TableView的单元格有另一个TableViewCustomCell
  • 是的……没错。

标签: ios swift uitableview


【解决方案1】:

首先为您的MainTableView 注册来自 Xib 的正常单元格 只需正常操作,DepartureDetailTableViewCell 将拥有内部单元格的所有数据源和委托:

别忘了写正确的单元格标识符等等

在 ViewController 中:

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {


    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.register(UINib.init(nibName: "DepartureDetailTableViewCell", bundle: nil), forCellReuseIdentifier: "DepartureDetailsTableViewCell")
        self.tableView.delegate = self
        self.tableView.dataSource = self

    }


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "DepartureDetailTableViewCell", for: indexPath) as! DepartureDetailTableViewCell
        return cell
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 4 // cell data source
    }
}

在 DepartureDetailTableViewCell:

class DepartureDetailTableViewCell: UITableViewCell {

    @IBOutlet weak var tableView:UITableView!

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

        self.tableView.register(UINib.init(nibName: "DepartureInsideTableViewCell", bundle: nil), forCellReuseIdentifier: "DepartureInsideTableViewCell")
        self.tableView.delegate = self
        self.tableView.dataSource = self
    }


}

extension DepartureDetailTableViewCell: UITableViewDelegate,UITableViewDataSource{

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "DepartureInsideTableViewCell", for: indexPath) as!  DepartureInsideTableViewCell
        return cell
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 4 // cell data source
    }
}

【讨论】:

  • 我不知道为什么,但在扩展DepartureDetailTableViewCell:说:“扩展DepartureDetailTableViewCell:”帮助!
  • 是的,你有什么问题
  • 对不起..这说:使用未声明的类型'DepartureInsideTableViewCell'
  • yes 添加您的单元格,您的单元格名称是什么DepartureInsideTableViewCell 替换为您自己的单元格名称
  • 如何分享你的代码我给你的想法是你应该确保创建正确的单元格并写下正确的名称
猜你喜欢
  • 2012-06-17
  • 2016-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多