Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Facing fatal error when I use double tableview in single view Controller

This is the error I am facing:

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

On this line:

cell!.nameLabel.text = priview.name

Here is my code:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    var count: Int?

    if tableView == MainTableView {
        count = maintableArray.count
    }

    if tableView == searchTableView {
        count = searchArray.count
    }

    return count!
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cellReturn : UITableViewCell?

    if tableView == MainTableView{
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? MainTableCell
        let preview = maintableArray[indexPath.row]
        cell?.detailLabelOne.text = preview.details
        cell?.detailslabelTwo.text = preview.price
        cell?.detailLabelThree.text = preview.otherInfo
        cell?.imageView?.image = preview.image

        cellReturn = cell!
    } else if tableView == searchTableView {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as? SearchTableCell
        let priview = searchArray[indexPath.row]

        cell!.nameLabel.text = priview.name

        cellReturn = cell
    }

    return cellReturn!
}

Comments