【问题标题】:How to create dynamic tableView within dynamic TableView in swift?如何在 swift 中在动态 TableView 中创建动态 tableView?
【发布时间】:2018-09-12 06:09:44
【问题描述】:

我想在动态表视图中快速创建动态表视图。

像这样, See Image

例如,在第一个 tableView 单元格的外部,制作 3 行表格单元格, 在第二个 tableView 单元格中,制作 1row 表格单元格,......像这样。

如果有其他方法可以创建此类视图,请告诉我。

【问题讨论】:

  • 我建议使用部分来充当每个“内部”tableView,并相应地自定义 UI。
  • @mmr118 您好,感谢您的回答。我添加了 tableView,但是如何返回每个单元格中不同的表格行数?
  • 在您的 numberOfRows 计数方法中检查 tableview 对象。这将帮助您确定要返回哪个表的行数。相同的逻辑将在 cellforrowatindexpath() 中起作用。检查此链接stackoverflow.com/questions/17398058/…
  • @Arti ,我不知道如何返回表格单元格。如果我有一个名为 countArray = [2,3,1,3,3] 的数组,我想返回 2 个行单元格, 3 行单元格,1 行单元格,3 行单元格,3 行单元格。是否可以返回 countArray[section] ?

标签: swift dynamic tableview tablecell


【解决方案1】:

这可以通过以下方式完成。使用具有多个部分的 tableview。您的部分计数将来自您要显示的嵌套数据数组,如下所示:

//MARK: custom tableView cell for header
class CustomHeaderCell: UITableViewCell {
     @IBOutlet weak var titleLabel: UILabel!
     @IBOutlet weak var descriptionLabel: UILabel!
}

//MARK: custom tableView cell for section 
class CustomCell: UITableViewCell {
     @IBOutlet weak var titleLabel: UILabel!
     @IBOutlet weak var descriptionLabel: UILabel!
}

//MARK: Custom model class
class MyClassModelData {
    private var title: String
    private var description: String
    private var mySubModel: [MySubModel]

    init(title: String, description: String, mySubModel: [MySubModel]){
        self.title = title
        self.description = description
        self.mySubModel = mySubModel
    }

    func getTitle() -> String {
        return self.title
    }
    func getDescription() -> String {
        return self.description
    }
    func getSubModelValues() -> [MySubModel] {
        retusn self.mySubModel
    }
}

//MARK: Custom submodel class
class MySubModel {
    private var title: String
    private var description: String

    init(title: String, description: String){
        self.title = title
        self.description = description
    }

    func getTitle() -> String {
        return self.title
    }
    func getDescription() -> String {
        return self.description
    }
}

//MARK: Section table view controller
class MyTableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    //Or use UITableViewController

    var sections = [MyClassModelData]()
    var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // The below line is to eliminate the empty cells
        self.tableView.tableFooterView = UIView()

        //Delegates for tableView
        self.tableView.delegate = self
        self.tableView.datasource = self        
    }

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 100 //Or UITableViewAutomaticDimension
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let section = self.sections[section]

        // Dequeue with the reuse identifier your custom cell or create new one
        let headerCell = self.customTableView.dequeueReusableHeaderFooterView(withIdentifier: "customHeaderCell") as! CustomHeaderCell
        headerCell.titleLabel.text = section.getTitle()
        headerCell.descriptionLabel.text = section.getDescription()

        return headerCell
    }

    // Give a height to our table view cell
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100 //Or UITableViewAutomaticDimension
    }

    // We have only one section
    func numberOfSections(in tableView: UITableView) -> Int {
        return sections.count
    }

    // Rows in section
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return sections[section].getSubModelValues().count
    }

    // Cell creation
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = self.customTableView.dequeueReusableHeaderFooterView(withIdentifier: "customCell") as! CustomCell
        let sectionData = sections[indexPath.section].getSubModelValues()[indexPath.row]

        cell.titleLabel = sectionData.getTitle()
        cell.descriptionLabel = sectionData.getDescription()

        return cell
    }

}

还可以看看这个帖子: https://medium.com/swift-programming/swift-enums-and-uitableview-sections-1806b74b8138

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-26
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-01
    • 1970-01-01
    相关资源
    最近更新 更多