Search code examples
iosswifttableviewcells

Data not showing in custom tableview cell in swift have added the cell class below


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
    }  
}

Solution

  • 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.