1

I want to create dynamic tableView within dynamic tableView in swift.

Like this, See Image

For example, in outside in first tableView cell, make 3-row table cell, in second tableView cell, make 1row table cell, ... like this.

If there is an another way to create these kinds of view, please let me know.

Luca D'Alberti
  • 4,749
  • 3
  • 25
  • 45
poq
  • 11
  • 1
  • 8
  • I suggest using sections to act as each "inner" tableView, and customizing the UI accordingly. – mmr118 Sep 12 '18 at 06:39
  • @mmr118 Hi, thank you for answering. I added tableView, but how can i return table rows count differently in each cells? – poq Sep 12 '18 at 06:52
  • Inside your numberOfRows count method place a check on tableview object. that will help you in identifying for which table you are returning row count. same logic will work in cellforrowatindexpath(). Check this link https://stackoverflow.com/questions/17398058/is-it-possible-to-add-uitableview-within-a-uitableviewcell – Arti Sep 12 '18 at 06:56
  • Check this... https://stackoverflow.com/a/29473112/472336 – Pramod Sep 12 '18 at 06:57
  • @Arti , I don't know how to return table cells.. if i have an array named countArray = [2,3,1,3,3] I want to return 2 row cells, 3 row cells, 1 row cells, 3 row cells, 3 row cells. Is it possible to return countArray[section] ? – poq Sep 12 '18 at 07:17
  • yes your countArray.count will be noofsections and each item at index i will become your rowcount. – Arti Sep 12 '18 at 07:54
  • @Arti func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return settlementsList[section] } } But When i do this, it returns only value in 0 index apply to all cells. It shows two rows in every cells. – poq Sep 12 '18 at 07:57
  • Follow the https://stackoverflow.com/questions/17398058/is-it-possible-to-add-uitableview-within-a-uitableviewcell. This should help you. – Arti Sep 12 '18 at 08:09

1 Answers1

1

This can be done in next way. Use tableview with multiple sections. Your section count will be from your nested array of data that you want to show like this:

//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
    }

}

Also take a look at this post: https://medium.com/swift-programming/swift-enums-and-uitableview-sections-1806b74b8138

Kenan Begić
  • 1,228
  • 11
  • 21