0

I have no idea what is wrong with my code. I researched around the Internet could not find a way to fix this.

I have two UITableView in same UIViewController. They both have their own UITableView classes and their identifiers assigned properly from the MainStoryboard (checked multiple times, connections are solid.)

inside viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self
    tableView.register(UITableViewCell.self, forCellReuseIdentifier: "PortfolioCell")
    barTableView.delegate = self
    barTableView.dataSource = self
    barTableView.register(UITableViewCell.self, forCellReuseIdentifier: "BarCell")

Related code:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if tableView == self.tableView {
        return selCur.count
    } else {
        return selCur.count
    }
}

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

    if tableView == self.tableView {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "PortfolioCell", for: indexPath) as? PortfolioCell else { return UITableViewCell() } 

                let noVal = selCur.count[indexPath.row].rank ?? 0
                let nameVal = selCur.count[indexPath.row].name ?? "N/A"

                cell.configureCell(no: noVal, name: nameVal)
            }
        }
        return cell

    } else {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "BarCell", for: indexPath) as? BarCell else { return UITableViewCell() }
                let noVal = selectedCurrencies[indexPath.row].rank ?? 0
                let nameVal = selectedCurrencies[indexPath.row].name ?? "N/A"

                cell.configureCell(no: noVal, name: nameVal)
            }
        }
        return cell
    }
}

I am guessing, cellForRowAt method returns regular UITableViewCell instead of the custom "PortfolioCell" or "BarCell". I also tried tableView.tag method as well and got the same result.

Thank you in advance.

1 Answer 1

1

You have to register each cell correctly.

tableView.register(PortfolioCell.self, forCellReuseIdentifier: "PortfolioCell")

or if you have a cell with .xib

tableView.register(UINib(nibName: "PortfolioCell", bundle: nil),
                   forCellReuseIdentifier: "PortfolioCell")

If cell is in storyboard, you don't even need to register it.

Sign up to request clarification or add additional context in comments.

3 Comments

Hello Ryan, thank you for the answer. After I register the cells as you shown above, now I started to get nil error inside my cell.configureCell method. This method is working properly on other VC using exact same configuration. Only difference is one VC has one tableView this one got two. Which leads me to thinking issue still continues. As you pointed out cells are in storyboard and I already set their Identifiers and Classes respectively. Any ideas?
Sounds like you have created a custom cell from UIStoryboard. If so, the cell is belonged to the specific view controller. You can't really reuse the cell in different viewController. If you want use a sepcific custome cell in many view controllers, you need to create as a separate xib or programatically.
Thank you so much for all your answers. Appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.