Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift cell content not showing

I have a tableView that contains cells and each cell should contain an image, a label and a non-editable textView.

Here's what it displays instead: https://i.stack.imgur.com/WmOn3.png As you can see it shows an empty tableView but with the three top lines closer to each other.

ViewController.swift:

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

let food = ["Pasta-salad", "Hamburger", "Pancakes"]

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return (food.count)
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell

    cell.foodImage.image = UIImage(named: (food[indexPath.row] + ".jpg"))
    cell.foodLabel.text = food[indexPath.row]
    cell.foodDescription.text = ("insert description of \(food[indexPath.row])")

    return (cell)
}
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}

TableViewCell.swift

class TableViewCell: UITableViewCell {

@IBOutlet weak var foodImage: UIImageView!
@IBOutlet weak var foodLabel: UILabel!
@IBOutlet weak var foodDescription: UITextView!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}
}

Main.storyboard: enter image description here

like image 670
random1234 Avatar asked Jan 29 '23 13:01

random1234


1 Answers

It seems like the height of cell 0.

Implement the UITableViewDelegate method for returning the height of each cell you can also return different height for each, by some calculation.

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 100.0;//Choose your custom row height
}
like image 110
Prateek Varshney Avatar answered Feb 04 '23 04:02

Prateek Varshney