1

I have tried everything possible but I still can't figure out why the data is not shown in a custom cell. It looks fine in a subTitle cell and println confirms the data is there. I have checked the identifier and the IBOutlets a 100 times:

//  ActivityDetailsTableViewController.swift
//  TestActForm
//
//  Created by Jeremy Andrews on 2015/08/08.
//  Copyright (c) 2015 Jeremy Andrews. All rights reserved.
//

import UIKit

class ActivityDetailsTableViewController: UITableViewController
{

    var activities: [Activity] = activityProfile

    override func viewDidLoad() 
    {
        super.viewDidLoad() 
    }

    override func didReceiveMemoryWarning() 
    {
        super.didReceiveMemoryWarning()
    }

    // MARK: - Table view data source

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

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


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
        -> UITableViewCell 
    {
        let cell = tableView.dequeueReusableCellWithIdentifier("ActivityDetail", forIndexPath: indexPath) as! ActivityDetailCell
        let activity = activities[indexPath.row] as Activity
        println(activity.question)
        cell.questionLabel?.text = activity.question
        cell.answerLabel?.text = activity.answer
        println(cell.questionLabel.text)
        return cell
    }

}

import UIKit

class ActivityDetailCell: UITableViewCell 
{
    @IBOutlet weak var questionLabel: UILabel!
    @IBOutlet weak var answerLabel: UILabel!


    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
    }  
}
LuckyStarr
  • 1,468
  • 2
  • 26
  • 39
Jeremy Andrews
  • 807
  • 12
  • 17

3 Answers3

2

Thanks to everyone who gave suggestions - after 2 days struggle I finally found the solution thanks to: creating custom tableview cells in swift.

All you have to do is go to file inspector - uncheck size classes - there will be warnings etc.run and there is the data - strangely - go back to file inspector and check "use size classes" again, run and all data correctly reflected. Seems like in some cases the margin is set to negative. A thought - if println( cell.nameLabel.text) shows your data is there then it is, check margin or put labels in the centre as a trial.

I noticed before that the story board had a highlighted area that did not match the cell boundaries - this actually showed the boundaries that run would use. A bug I think

Community
  • 1
  • 1
Jeremy Andrews
  • 807
  • 12
  • 17
1

Use a breakpoint in cellForRowAtIndexPath and check cell, outlets, model. Also check if you set the delegate, datasource and outlet for the table.

Lucian Boboc
  • 480
  • 2
  • 6
0
cell.questionLabel?.text = activity.question
cell.answerLabel?.text = activity.answer

this code would fail silently if these label are not created. I bet you did not hook them up via IBOutlets in your storyboard.

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
  • Thanks for the comments so far - but still stuck. What I don't understand is that the println in this section of code shows clearly that cell.questionLabel?.text and cell.answerLabel?.text has the required data cell.questionLabel?.text = activity.question cell.answerLabel?.text = activity.answer println(cell.questionLabel.text) return cell – Jeremy Andrews Aug 08 '15 at 19:08
  • Have added it at the end of the original post – Jeremy Andrews Aug 09 '15 at 08:18
  • I am still convinced that you did not hook up the labels in the storyboard with the iboutlets in your cell class. What happens if you write `cell.questionLabel!.text = activity.question` instead of `cell.questionLabel?.text = activity.question`. If it leads to a crash it proves the label is never created. – vikingosegundo Aug 09 '15 at 13:24
  • I am also convinced its to do with the IBOutlet and I am re-working everything now and will try your suggestion. I have a similar App table from a web tutorial which works fine and I further note that if I pout a blank label in this cell it shows up without being linked. In the problem table this does not happen so it would to have something to do with custom cell. – Jeremy Andrews Aug 09 '15 at 15:10
  • did you try to crash it, as I said? this would ultimately prove that your labels are not existent. – vikingosegundo Aug 09 '15 at 17:04
  • No but I found the solution and have answered my question. Thanks for the input - it was a case of Xcode setting negative margins for some reason – Jeremy Andrews Aug 09 '15 at 20:50
  • I-m having the same issue...I hooked the outlets but on the cell class....where else should I create them?? – John Sep 13 '17 at 19:54