NL97
NL97

Reputation: 79

UITableView does not populate with data

I am trying to make a simple UITableView with custom cells (including two labels and one image view) but I can't get the table to be populated with data.

I have created the following struct for my data:

struct feed {
  var title: String
  var subtitle: String
  var image: String
}

and here I have defined some sample data:

var myfeed = [feed(title: "Test Feed", subtitle: "Welcome to feedFeed", image: "https://www.demo.com/imnage1.png"), feed(title: "Number 2", subtitle: "Demo?", image: "https://www.demo.com/imnage2.png")]

I have created an UITableView in my Storyboard, have configured the Custom cell and am using the Cell identifier "LabelCell". I have created a separate cocoaTouchclass file for the UITableViewCell class:

import UIKit

class ehappyTableViewCell: UITableViewCell {

    @IBOutlet weak var headlineTitleLabel: UILabel! 
    @IBOutlet weak var headlineTextLabel: UILabel!
    @IBOutlet weak var headlineImageView: UIImageView!

    override func awakeFromNib() {
       super.awakeFromNib()
       func tableView(_ tableView: UITableViewCell, heightForRowAt indexPath: IndexPath) -> CGFloat
           { 
              return 100.0;//Choose your custom row height
            }
         // Initialization code
    }    

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        // Configure the view for the selected state
    }

}

In my ViewController file I have the following code:

    @IBOutlet weak var table: UITableView!    

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

    func tableView(_ tableViwq: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:ehappyTableViewCell = self.table.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath) as! ehappyTableViewCell!

        let headline = myfeed[indexPath.row]
        cell.headlineTitleLabel?.text = headline.title
        cell.headlineTextLabel?.text = headline.subtitle
        let urlWithoutHTTP = headline.image
        let httpAddition = "https:"
        let addition = "\(httpAddition)\(urlWithoutHTTP)"

        let url = URL(string: addition)

        DispatchQueue.global().async {
           let data = try? Data(contentsOf: url!) //make sure your image in this url does exist, otherwise unwrap in a if let check / try-catch
            DispatchQueue.main.async {
                cell.headlineImageView.image = UIImage(data: data!)
            }
        }
        return cell
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 140.0;//Choose your custom row height
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.table.delegate = self
        self.table.dataSource = self
    }

When I run the app the table will not populate with the sample data I have created. Any suggestions on what I can do?

Upvotes: 0

Views: 120

Answers (1)

R. Shah
R. Shah

Reputation: 56

  • If your cell is an xib then you have to register the cell by writing the following code in the viewDidLoad

    self.table.register(UINib(nibName: "ehappyTableViewCell", bundle: nil), forCellReuseIdentifier: "LabelCell")

  • If it is designed inside the viewcontroller in storyboard then no need to register

  • Check if delegate and dataSource are given

  • Try replacing the line with following line

    let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell") as? ehappyTableViewCell

Upvotes: 1

Related Questions