我有一个自定义表格视图单元格。在故事板中,我实施了UILabel
和UIButton
。我希望每次重复使用时都给标签赋予不同的值。故事板连接很好。如果我使用cell.textLabel.text = episodeTitle
那么可行,但如果我设置了UILabel
的text属性,那么我会收到错误
fatal error: unexpectedly found nil while unwrapping an Optional value
我已经尝试过注册课程,但这不起作用。不知道该怎么做。 SO上有很多类似的帖子,但没有一个帮助。
这是我的cellForRowAtIndexPath
:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//tableView.registerClass(episodeCell.self, forCellReuseIdentifier: "episode")
var cell = tableView.dequeueReusableCellWithIdentifier("episode", forIndexPath: indexPath) as episodeCell
cell = episodeCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "episode")
let episode: MWFeedItem? = episodesToDisplay.objectAtIndex(indexPath.row) as? MWFeedItem
if episode != nil {
//process the episode
var episodeTitle: NSString = episode?.title as String!
//cell.textLabel.text = episodeTitle
cell.episodeTitle.text = episodeTitle
}
return cell
}
这是我的自定义单元格:
class episodeCell: UITableViewCell {
var progress: Float?
@IBOutlet weak var episodeTitle: UILabel!
}
答案 0 :(得分:0)
错误在于:
var cell = tableView.dequeueReusableCellWithIdentifier("episode", forIndexPath: indexPath) as episodeCell
cell = episodeCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "episode")
您将单元格出列并分配给cell
变量,然后将该实例替换为全新的变量。删除这一行:
cell = episodeCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "episode")