ios - 如何在swift中的动态tableview中创建动态tableview?

标签 ios swift uitableview dynamic tableview

这就是我想做的。 我想要里面的tableview,返回动态计数。

例如,第一个单元格中有 3 行,第二个单元格有 2 行,第三个单元格有 4 行......就像这样。

而且 tableview 之外的计数也是动态的。

我应该返回什么tableView单元格计数?

enter image description here

最佳答案

我认为你应该为 2 个 TableView 创建 2 个数据源。就像这样:

final class YouViewController: UIViewController {
    @IBOutlet weak var tableView: UITableView!
    let outerModel: [Any] = []
    lazy var outerDatasource = OuterTableDatasource(model: self.outerModel)
    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.dataSource = outerDatasource
        self.tableView.delegate = outerDatasource

    }
}

final class CellThatContainTableView: UITableViewCell {
    @IBOutlet weak var tableView: UITableView!
    var dataSource: InnerTableDatasource! {
        didSet {
            self.tableView.dataSource = dataSource
            self.tableView.delegate = dataSource
        }
    }
    func configure(dataSource: InnerTableDatasource) {
        self.dataSource = dataSource
    }
}

final class OuterTableDatasource: NSObject {
    var model: [Any]
    init(model: [Any]) {
        self.model = model
    }
}

extension OuterTableDatasource: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return model.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //implement cell configuration
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CellThatContainTableView
        cell.configure(dataSource: InnerTableDatasource(model: []))
        return cell
    }


}
extension OuterTableDatasource: UITableViewDelegate {

}

final class InnerTableDatasource: NSObject {
    var model: [Any]
    init(model: [Any]) {
        self.model = model
    }
}

extension InnerTableDatasource: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return model.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //implement cell configuration
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!


        return cell
    }


}
extension InnerTableDatasource: UITableViewDelegate {

}

关于ios - 如何在swift中的动态tableview中创建动态tableview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52289949/

相关文章:

ios - UIImageView .scaleAspectFit 和自动布局无法从 Swift 以编程方式工作

swift - iOS : WKWebView Does Not Display SVG

ios - Swift 自定义表格单元格

ios - 在iOS中禁用tableview中的多选单元格

iphone - NSInvalidArgumentException与UITableView吗?

ios - 在图像上渲染文本,以全分辨率保存图像?

iOS , ld : framework not found GoogleMaps for architecture arm64

ios - 接收方在 self.performSegue(withIdentifier, sender) 中没有带有标识符的 segue

ios - 为什么测试的时候会调用runloop

iphone - 从 UITableView 中取出可重用单元格