Two Tables on One View in Swift

How do I create two table views in one view controller with two custom UITableViewCells?

The error appears because if for any reason, the table view is non of the two options that you wrote, then it doesn't have any value to return, just add a default value at the end:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == firstTableView,
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomOne") as? CustomOneTableViewCell {
return cell
} else if tableView == autoSuggestTableView,
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTwo") as? CustomTwoTableViewCell {
return cell
}

return UITableViewCell()
}

Updated to swift 4.1.2:
I've updated this answer to version 4.1.2, also, because the return value of the method cannot be nil, modified to a default, dummy UITableViewCell.

Multiple TableViews in One UIView Swift - How to show both

Unfortunately in my case it was due to my constraints as I had put them into a stack view but with the stack view not set to distribute correctly. So I needed to amend that setting in the storyboard. Then both were present and correct as expected.

Two tableviews in one view controller, one table view not showing up

If you do not need any section in pickerTableView, then also it has to have at least one section.

So return 1 in method numberOfSections when tableview == self. pickerTableView.

func numberOfSections(in tableView: UITableView) -> Int {
if tableView == self.tableView {
return Data.userModels.count
} else {
return 1
}
}


Related Topics



Leave a reply



Submit