使用故事板在swift中创建自定义TableViewCell
当前正在使用Xcode版本6.0.1并尝试创建自己的自定义tableViewCell。 我已经设置了XIB文件出现像这样使用故事板在swift中创建自定义TableViewCell
代码RecipeTableViewCell类
import UIKit
类RecipesTableViewCell:{UITableViewCell的
@IBOutlet weak var recipeImage: UIImageView!
@IBOutlet weak var nameLabel:UILabel!
@IBOutlet weak var prepTimeLabel: 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
}
}
在我tableViewController类我的代码看起来像这个控制单元格的主要方法。
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var cell:RecipesTableViewCell? = tableView.dequeueReusableCellWithIdentifier("cell2") as? RecipesTableViewCell
if cell == nil{
//var nib:NSArray = NSBundle.mainBundle().loadNibNamed("RecipesTableViewCell", owner: self, options: nil)
var tempController:UIViewController = UIViewController(nibName: "RecipesTableViewCell", bundle: nil)
cell = tempController.view as? RecipesTableViewCell
println(cell)
}
cell?.prepTimeLabel.text = "5 mins"
cell?.nameLabel.text = "HellO"
return cell!
}
任何时候,我运行它给我的应用程序
致命错误:意外发现零而展开的可选值 (LLDB)
你的cellForRowAtIndexPath:应该是这样的,
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var cell:RecipesTableViewCell? = tableView.dequeueReusableCellWithIdentifier("cell2") as? RecipesTableViewCell
if cell == nil{
let nib = UINib(nibName: "RecipesTableViewCell", bundle: nil)
let allViews: [AnyObject]? = nib?.instantiateWithOwner(self, options: nil)
cell = allViews?.last as? cell:RecipesTableViewCell
}
cell?.prepTimeLabel.text = "5 mins"
cell?.nameLabel.text = "HellO"
return cell!
}
但是,最好使用register nib,然后使用tableView.dequeueReusableCellWithIdentifier:forIndexPath:method。
我的xib文件名叫做RecipeTableCell.xib(只是说它可能有助于回答Q) 所以我复制你的版本的方法,我得到这个错误 ***终止应用程序由于未捕获异常'NSInternalInconsistencyException',原因:'无法在我的.xib文件中加载NIB捆绑:' 我通过cellResuseIdenfier命名为cell2,并在故事板文件中删除了原型tableViewCell,以便它只是一个空表View。 – stringRay2014 2014-10-19 12:39:01
你在你的UITableViewController类注册笔尖:
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerNib(UINib(nibName: "RecipesTableViewCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: "cell2")
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:RecipesTableViewCell? = tableView.dequeueReusableCellWithIdentifier("cell2") as? RecipesTableViewCell
cell?.prepTimeLabel.text = "5 mins"
cell?.nameLabel.text = "HellO"
return cell!
}
当我跑我们的代码时得到这个错误 ***由于未捕获的异常'NSInternalInconsistencyException',原因:'无法加载包中的NIB: 与上述答案同样错误! 仅供参考我的.xib文件被称为RecipeTableCell.xib,而我的自定义TableViewCell类被称为RecipesTableViewCell – stringRay2014 2014-10-19 12:44:53
您是否已验证您的nib文件是否位于复制资源构建步骤?我在Xcode 6上遇到了一些问题,无法正确复制笔尖,因此值得检查该笔包并检查笔尖是否存在。 – 2014-10-19 12:59:59
@LevLandau如何验证笔尖文件? – stringRay2014 2014-10-19 19:46:10
如果在调用崩溃'dequeueReusableCellWithIdentifier'它可能是你没有正确设置你的小区标识。由于检查员在屏幕上不可见,我不能说是否是这种情况。同时检查您是否已将所有插座连接到正确的目标。尽管他们应该有点点滴滴,但你可能已经将两个网点连接到同一个目标。 – 2014-10-19 11:18:19