CornFlakes
CornFlakes

Reputation: 1

How to fix Table View Cells not showing on launch?

Greetings fellow reader, to start this all with, I need to say, I have read a lot of similar posts on here with almost the same problem and have implemented a lot of code from here in my project and I can surely say that I am on the same page still, blank table view.

I am trying to load data from an API call to the table view cells, primarily only titles (only text) and I am successful with completing the API call and storing the data of the API call into an array which I will use to access data from.

What I have tried: - alternating between fixed and automatic row height - loading up tableView.delegate and tableView.dataSource on viewDidLoad(), also setting the delegate and dataSource in storyboard - alternating between using a forEach() method and using for 'item' in... - setting up breakpoints but I do not get to them at all, for example I do not get to any breakpoints like 'func tableView(_ tableview:...)' nor is the data from those functions printed in console - putting an identifier at the storyboard Table View Cell and using that identifier to print out cells - using .reloadData() in viewWillLoad()

extension HomeViewController: UITableViewDelegate {

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        let item = items[indexPath.row]
        print("Selected Item: \(item)")
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100.0;
    }
}

extension HomeViewController: UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        print("CURRENT INDEX PATH BEING CONFIGURED: \(indexPath)")
        let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: ShowsTableViewCell.self), for: indexPath) as! ShowsTableViewCell
            cell.configure(with: items[indexPath.row])

//        let something: Show
//        something = items[indexPath.row]
//
//        cell.titleTVShow.text = something.title
          return cell
    }
}
 switch response.result {
                case .success(let shows):
                    print("Succes: \(shows)")
                    shows.forEach { show in
                        self.items.append(show)
                    }
                case .failure(let error):
                    print("Eror: \(error)")
                }
extension ShowsTableViewCell {
    func configure(with item: Show) {
        titleTVShow.text = item.title
        thumbnailTVShow.image = nil
    }
}
struct Show: Codable {
    let idShow: String
    let title: String
    let imageUrl: String
    let likesCount: Int

    enum CodingKeys: String, CodingKey {
        case idShow = "_id"
        case title
        case imageUrl
        case likesCount
    }
}

I do not get any errors. The API proceeds with a success and prints out data from the json in console. I expect to load the data from the API call in the tableView but that is not happening.

Upvotes: 0

Views: 525

Answers (1)

Arpit Dhamane
Arpit Dhamane

Reputation: 513

Try debugging first that if the table view delegate and data source methods are called or not. Then reloading the table view after the API response is stored in the array. After this, self.items.append(show).

Try this.

 switch response.result {
            case .success(let shows):
                print("Succes: \(shows)")
                shows.forEach { show in
                    self.items.append(show)
                }
                self.tableView.reloadData()
            case .failure(let error):
                print("Eror: \(error)")
            }

Upvotes: 1

Related Questions