在swift的自定义tableview单元格中没有显示的数据添加了下面的单元格类

我已经尝试了一切可能,但我仍然不知道为什么数据不显示在自定义单元格中。 它在子subTitle单元格中看起来很好, println确认数据在那里。 我已经检查了标识符和IBOutlets 100次:

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

 cell.questionLabel?.text = activity.question cell.answerLabel?.text = activity.answer 

如果这些标签没有被创build,这个代码将默默地失败。 我敢打赌,你没有通过IBOutlets把它们连接到你的故事板。

感谢所有提供build议的人们 – 经过2天的努力,我终于find了解决scheme,感谢: 在swift中创build自定义tableview单元格 。

所有你需要做的是去文件检查员 – 取消选中大小class级 – 会有警告etc.Run和有数据 – 奇怪 – 回到文件检查员,再次检查“使用大小class级”,运行和所有数据正确地反映。 似乎在某些情况下,保证金设置为负值。 一个想法 – 如果println(cell.nameLabel.text)显示你的数据在那里,那么检查边界或把标签放在中间作为一个试用。

之前我注意到故事板有一个与单元格边界不匹配的突出区域 – 这实际上显示了将运行的边界。 我觉得一个错误

cellForRowAtIndexPath使用断点并检查单元格,sockets,模型。 另外检查你是否设置表的委托,数据源和出口。