use*_*890 0 uitableview custom-cell ios swift
cellForRowAtIndexPath
当我尝试创建自定义表格单元格时,我的程序在方法中不断崩溃。我在代码的每一行都放置了一个断点,在我尝试创建带有错误 的单元格变量后代码失败:unexpectedly found nil while unwrapping optional value
。我以前曾多次成功使用自定义表格单元格,但从未遇到过此问题。这看起来很简单,但我无法弄清楚出了什么问题。也许 xcode 中的某些内容发生了我不知道的变化?任何帮助表示赞赏。谢谢!...此外,我确实在viewDidLoad
. 我也会包含该代码。
var nib = UINib(nibName: "FeedTableViewCell", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "feedCell")
Run Code Online (Sandbox Code Playgroud)
问题代码如下:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:FeedTableViewCell = tableView.dequeueReusableCellWithIdentifier("feedCell", forIndexPath: indexPath) as! FeedTableViewCell
cell.descriptionLabel.text = "Testing the description label of my cell."
return cell
}
Run Code Online (Sandbox Code Playgroud)
更新
这个项目和其他已经成功的项目之间的一个区别是 Xcode 现在提示我在 as 后面放一个 bang (!),而之前我从未使用过。如果我不添加 (!) 它会给我错误消息“AnyObject 不可转换为FeedTableViewCell
”
作为!FeedTableViewCell
- 而不是 - 作为FeedTableViewCell
FeedTableViewCell 类
import UIKit
class FeedTableViewCell: UITableViewCell {
@IBOutlet weak var descriptionLabel: UILabel!
@IBOutlet weak var priceLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
priceLabel.font = UIFont(name: "AvenirNext-Bold", size: 30.0)
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
Run Code Online (Sandbox Code Playgroud)
在花了太多时间试图让它工作之后。我修改了我的自定义表格单元格,创建了一个全新的表格单元格,以完全相同的方式进行设置,现在一切正常。仍然不知道发生了什么,但我想从中得到的教训是,有时你只需要核对它并重新开始。
归档时间: |
|
查看次数: |
2610 次 |
最近记录: |