JLO JLO - 6 months ago 75
iOS Question

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

i am trying to create two UITableViews in one view controller using two custom UITableViewCells. I have the following:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if tableView == self.tableView {
let cell = tableView.dequeueReusableCellWithIdentifier("CustomOne") as! CustomOneTableViewCell
return cell
}

if tableView == self.autoSuggestTableView {
let cell = tableView.dequeueReusableCellWithIdentifier("CustomTwo") as! CustomTwoTableViewCell
return cell
}
}


But I keep getting the error:

Missing return in a function expected to return 'UITableViewCell'


What do I have to return in the end of the method?

Answer

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 return nil at the end:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if tableView == self.tableView {
        let cell = tableView.dequeueReusableCellWithIdentifier("CustomOne") as! CustomOneTableViewCell
        return cell
    } else if tableView == self.autoSuggestTableView {
        let cell = tableView.dequeueReusableCellWithIdentifier("CustomTwo") as! CustomTwoTableViewCell
        return cell
    }

    return nil
}
Comments