1

I want to implement a simple tableView in my Viewcontroller but the output is not complete. The content is just visible in one sometimes in two rows.

The classic things:

  • The class use this:

    class MealOfWeekView: UIViewController, UITableViewDelegate, UITableViewDataSource  {...}
    
  • I set the delegates

    override func viewDidLoad() {
       super.viewDidLoad()
       self.tableViewFood.delegate = self
       self.tableViewFood.dataSource = self
       self.tableViewFood.reloadData() 
    }
    
  • I use the right identifier:

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
       let cell = tableView.dequeueReusableCellWithIdentifier("foodIdent", forIndexPath: indexPath) as! FoodTableViewCell
       cell.dayLabel?.text = "\(day[indexPath.row])"
       return cell
    }
    
  • return 1 section and return 7 rows

=> I use the first time the Tab Bar Controller, in my first tab there is already a tableView. This one works perfect.

The tableView shows as far as I know the days tuesday, saturday or sunday... don't know, whether the info is important :)

EDIT So with your help I figured out, that my daylabel is nil.

My FoodTableViewCell

class FoodTableViewCell: UITableViewCell {
 @IBOutlet weak var dayLbl: UILabel!
}

I add to my viewDidLoad this line:

self.tableViewFood.registerClass(FoodTableViewCell.self, forCellReuseIdentifier: "foodIdent")

But it doesn't work.

If you need more code, give a sign.

Thank you!

Looks like this: enter image description here

kuzdu
  • 7,124
  • 1
  • 51
  • 69
  • Add `print("day: \(day[indexPath.row])")` right below the line with `cell.dayLabel?.text = "\(day[indexPath.row])"` and see what you get there. – dasdom Dec 16 '15 at 20:44
  • How many objects do you have in your `day` array? You can use `print(day.count)` to figure it out – Laurent Rivard Dec 16 '15 at 20:54

2 Answers2

0

Your issue is with your custom class FoodTableViewCell, but I would verify the following first.

  1. Confirm that the label is being set with the day of the week for the row index. You can do this by setting a breakpoint or printing out statements such as where you create your cells.

print("dayLabel: (cell.dayLabel?.text)")

print("day[indexPath.row]: (day[indexPath.row]")

  1. Confirm you are registering the FoodTableViewCell with the table view.

  2. Confirm that your subclass FoodTableViewCell's dayLabel property is setup correctly. Try changing the background color so you know it is least being displayed in the UI.

  3. Check that your subclass of UITableViewCell is overriding and setting up the dayLabel for reuse correctly.prepareForReuse()

Background information for working with table views

Peter Hornsby
  • 4,208
  • 1
  • 25
  • 44
0

SOLUTION

I found my answer here

When you use Tab Bar Controller you have to use viewDidAppear or viewWillAppear

this lines worked for me:

override func viewWillAppear(animated: Bool) {
    self.tableViewFood.delegate = self
    self.tableViewFood.dataSource = self


    dispatch_async(dispatch_get_main_queue(), {
        self.tableViewFood.reloadData()
    })

}
Community
  • 1
  • 1
kuzdu
  • 7,124
  • 1
  • 51
  • 69