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
How to Simulate Mouse Click from MAC App to Other Application
How to Detect When Url of Amp Page Changed with Wkwebview
Xcode Beta 6 "Type of Expression Is Ambiguous Without More Context" Navigationlink
Error: Use of Unresolved Identifier 'Process'
Autolayout Contraints for a View from Xib
Uitextfield Setting Maximum Character Length in Swift
Swift - Firebase Search in Database
Writing Data to an Nsoutputstream in Swift 3
Swift 2 Migration Savecontext() in Appdelegate
How to Line Break Long Large Title in iOS 11
Programmatically Creating Constraints Bound to View Controller Margins
Xcode 8.3 Can't Support Swift 2.3
In Swift, Can You Split a String by Another String, Not Just a Character
Swiftyjson - Call Can Throw, But It Is Marked with 'Try' and the Error Is Not Handled
Uicollectionviewlayout Not Working with Uiimage in Swift 5 and Xcode 11
How to Validate Dynamically Added Textfields on a Button Click in Swiftui
Showing Cells in Demands in Uicollectionview with Vertical Infinite Scroll