问题
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
}
}
回答1:
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.
回答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
回答3:
Use a breakpoint in cellForRowAtIndexPath
and check cell, outlets, model.
Also check if you set the delegate, datasource and outlet for the table.
来源:https://stackoverflow.com/questions/31891825/data-not-showing-in-custom-tableview-cell-in-swift-have-added-the-cell-class-bel