Jimmy
Jimmy

Reputation: 137

Second tableview in UITableViewController

In my storyboard, I have a UITableViewController with static cell and 4 sections.

In the row of Section 4, I added into a UITableView (table2) with dynamic cell (no header, 1 section only).

So I followed some posts, and had some codes below:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    if tableView == table2 {
        return 1
    }
    return super.numberOfSectionsInTableView(tableView)
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if tableView == table2 {
        return 4
    }
    return super.tableView(tableView, numberOfRowsInSection: section)
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if tableView == table2 {
        let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as! CustomerCellView
        cell.setCustomCell()
        return cell
    }
    return super.tableView(tableView, cellForRowAtIndexPath: indexPath)
}

When run it, it builds successfully. The default table shows correctly, but table2 does not. table2 seemed to follow the format of the default table, so the problems are

If anyone knows what problem my code has, or anyone knows how to add a second tableview in UITableViewController

Upvotes: 1

Views: 564

Answers (2)

Jimmy
Jimmy

Reputation: 137

After a day, I figured out a way to solve this problem. Inspired by these posts:

So instead of putting a UITableView in the static cell, I put a Container View inside of the cell (in my case, the first row of Section 4), which embedded to another UITableViewController or UIViewController depends on your needs. The corresponding tableview codes go to another new file. And the way to set up the new table is as usual.

Upvotes: 1

Pie Er
Pie Er

Reputation: 5

You would have to create an uiviewcontroler and add 2 table views in that view. Then you can link them to the view controller with the data source and delegate. Then add the delegate in the view controllers class. After add in the normal functions for a table view.. You can use reeuse identifyers to classify which one needed.

Best of luck, Aj

Upvotes: 0

Related Questions