Two table views in single view controller

I have two table views in a single view contorller and want to be able to make a selection in the first table view and then show the details in the second table view. I have tried may options with no success. I assume it starts in the “didSelect”. Thank you in advance for your help.

Hi @jport1130, thanks for posting your issue here. It would be helpful if you posted code here for us to see how you have the didSelectRowAtIndexPath setup and or tell us a bit more as to what you’ve already tried. Also, what details are you attempting to pass through?

Best,
Gina

Hi Gina, Thank you for the response! I posted some code below. Please let me know your thoughts.

I was doing some research and one post mentioned it isn’t recommended to have two table views in one view controller. Do you have any thoughts about this?

@IBOutlet weak var estimateTableView: UITableView! //tag = 1
@IBOutlet weak var takeOffTableView: UITableView! //tag = 2

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) → UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: “Cell”, for: indexPath)
print(“CELL WAS TAPPED MF”)
if (tableView.tag == 1) {
let item = dataModel.estimateLists[indexPath.row]
configureText(for: cell, with: item)
} else if (tableView.tag == 2) {
let item = estimateList.takeOffList[indexPath.row]
configureTakeOffText(for: cell, with: item)
} else if !hasSearched {
let item = dataModel.estimateLists[indexPath.row]
configureText(for: cell, with: item)
} else if searchResults.count == 0 {
let item = dataModel.estimateLists[indexPath.row]
configureText(for: cell, with: item)
} else {
let item = searchResults[indexPath.row]
let label = cell.viewWithTag(3000) as! UILabel
label.text = item.estName
let label1 = cell.viewWithTag(3001) as! UILabel
label1.text = item.estDueDate
}
cell.accessoryType = .detailDisclosureButton
return cell
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) → Int {
if dataModel.estimateLists.first == nil {
return 1
} else if (tableView.tag == 1) {
return dataModel.estimateLists.count
} else if (tableView.tag == 2) {
return estimateList.takeOffList.count
} else if !hasSearched {
return dataModel.estimateLists.count
} else if searchResults.count == 0 {
return dataModel.estimateLists.count
} else {
return searchResults.count
}
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let est = dataModel.estimateLists[indexPath.row]
let selectedCell: UITableViewCell = tableView.cellForRow(at: indexPath)!
selectedCell.contentView.backgroundColor = UIColor.blue

    takeOffTableView.reloadData()

}

Hi @jport1130,
You can use multiple tableviews, however each tableView is backed by a DataSource and Delegate, you will have to task this to an object that handles the DataSource and Delegate, if you try to add the code to the ViewController, then you would end up with some interesting situations.

cheers,

Jayant

This topic was automatically closed after 166 days. New replies are no longer allowed.