LEKYSMA
LEKYSMA

Reputation: 141

unable to display text label in table view

i am building a table view with custom cells. in time the cell should contain various labels, but for the moment, there is only one label (serves as a test).

when I run the app, everything looks ok, however the cell.textLabel.text does not appear in the app. Each cell is blank.

for the data displayed, I built a model in a separate file visible here :

import Foundation

class CountryDataModel {
    let countryName: String
    let casesData: String
    let deathsData: String
    let recoveriesData: String
    
    init(country: String, cases: String, deaths: String, recoveries: String) {
        
        countryName = country
        casesData = cases
        deathsData = deaths
        recoveriesData = recoveries
        
    }
}

here is the view controller for the table view :

import UIKit

class PaysTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        
        listOfCountryAndRelatedData()
        
    }
    
    //MARK: - Relevant data
    var countryList = [CountryDataModel]()

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 0
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return countryList.count
    }

    //
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

        // Configure the cell...
        let indexPath = countryList[indexPath.row]
        
        cell.textLabel?.text = indexPath.countryName

        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("success")
    }

 
    func listOfCountryAndRelatedData() {
        
        let country1 = CountryDataModel(country: "Algeria", cases: "0", deaths: "0", recoveries: "0")
        countryList.append(country1)
        
        let country2 = CountryDataModel(country: "Bahamas", cases: "0", deaths: "0", recoveries: "0")
        countryList.append(country2)
        
        let country3 = CountryDataModel(country: "Seychelles", cases: "0", deaths: "0", recoveries: "0")
        countryList.append(country3)
        
        
        
    }

}

Upvotes: 0

Views: 124

Answers (1)

Dilan
Dilan

Reputation: 2688

set the numberOfSections as 1

override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
}

Then refresh the your Table after add data to your countryList

func listOfCountryAndRelatedData() {
 //add your data to arry

   self.tableView.reloadData()

}

Upvotes: 1

Related Questions