我有一个自定义的表视图单元格。在故事板中,我实现了aUILabel
和aUIButton
。我想给标签一个不同的值,每次它被重用。故事板连接很好。如果我使用cell.textLabel.text = episodeTitle
则有效,但如果我设置UILabel
的文本属性,则会得到错误
fatal error: unexpectedly found nil while unwrapping an Optional value
我试过注册一门课,但那不管用。不知道该怎么办了。类似的帖子有很多,但都没有帮助。
这是我的
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!
}
最佳答案
错误就在这里:
var cell = tableView.dequeueReusableCellWithIdentifier("episode", forIndexPath: indexPath) as episodeCell
cell = episodeCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "episode")
将单元格出列并分配给
cell
变量,然后用一个全新的实例替换该实例。删除此行:cell = episodeCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "episode")