【问题标题】:UITableView Prototype Cell error EXC_i386_INVOPUITableView 原型单元错误 EXC_i386_INVOP
【发布时间】: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


【解决方案1】:

已修复:好像我忘记在情节提要中添加引用插座

【讨论】:

    猜你喜欢
    • 2015-11-21
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 2014-05-18
    • 1970-01-01
    • 1970-01-01
    • 2018-04-14
    • 2020-12-31
    相关资源
    最近更新 更多