【发布时间】:2015-04-06 19:26:00
【问题描述】:
您好,我开始为一个小项目学习 swift,我正在使用 UITableView,但是当我尝试注册一个包含我的原型单元 (vwTblCell.xib) 的 nib 时出现错误
线程 1:EXC_BAD_INSTRUCTION (code=EXC_i386_INVOP,subcode=0x0)
这是我得到错误的地方:
tableView.registerNib(nib, forCellReuseIdentifier: "Cell")
这是我使用的代码。
ViewController.swift
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var tableView: UITableView!
override func viewDidLoad() {
var nib = UINib(nibName: "vwTblCell", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "Cell")
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:TblCell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as TblCell
cell.lblCell.text = self.items[indexPath.row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println("Row \(indexPath.row) selected")
}
func tableView(tableView:UITableView!, heightForRowAtIndexPath indexPath:NSIndexPath)->CGFloat
{
return 44
}
}
TblCell.swift
class TblCell: UITableViewCell {
@IBOutlet weak var lblCell: UILabel!
@IBOutlet weak var btnCell: UIButton!
override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
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
}
}
【问题讨论】:
-
你委托了吗?
-
您缺少以下委托-->class yourClass: UITableViewDelegate, UITableViewDataSource { // ... }'
-
我已经有代表我忘了在问题中添加他们
标签: ios uitableview swift