线程1:EXC_BAD_INSTRUCTION错误

时间:2015-02-14 23:49:48

标签: ios uitableview swift custom-cell

您好我一直在尝试编写UITableView自定义单元格,当我运行它时,我不断在这行代码中收到错误“Thread 1:EXC_BAD_INSTRUCTION(code = EXC_1386_INVOP,subcode = 0x0)”:

let cell: CustomCell = tableView.dequeueReusableCellWithIdentifier("Cell") as CustomCell

我的完整代码位于

之下

在ViewController.swift中:

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var mytableview: UITableView!

var arrayOfPersons: [Person] = [Person]()


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    self.setUpPersons()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func setUpPersons()
{
    var person1 = Person(name: "Anna", number: 60, imageName: "testingimage.jpg")
    var person2 = Person(name: "Joes", number: 10, imageName: "testingimage2.jpg")

    arrayOfPersons.append(person1)
    arrayOfPersons.append(person2)

}

 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
 {
    return arrayOfPersons.count

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell: CustomCell = tableView.dequeueReusableCellWithIdentifier("Cell") as CustomCell



    if indexPath.row % 2 == 0
    {
        cell.backgroundColor = UIColor.darkGrayColor()
    }
    else
    {
        cell.backgroundColor = UIColor.blackColor()
    }

    let person = arrayOfPersons [indexPath.row]

    cell.setCell(person.name, rightlabelInt: person.number, imageName: person.imageName)



    return cell

}

}

在CustomCell.Swift中:

import UIKit

class CustomCell: UITableViewCell {

@IBOutlet weak var leftlabel: UILabel!

@IBOutlet weak var rightlabel: UILabel!

@IBOutlet weak var myimageview: UIImageView!


required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)    }


override init(style: UITableViewCellStyle, reuseIdentifier: String?){
    super.init(style: style, reuseIdentifier: reuseIdentifier)

}

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
}

func setCell(leftlabelText: String, rightlabelInt: Int, imageName: String)
{
    self.leftlabel.text = leftlabelText
    self.rightlabel.text = String(rightlabelInt)
    self.myimageview.image = UIImage(named: imageName)
}

}

在Person.Swift中:

import Foundation

class Person {

var name = "name"
var number = 0
var imageName = "blank"

init(name: String, number: Int, imageName: String)
{
    self.name = name
    self.number = number
    self.imageName = imageName
}
}

我不确定错误意味着什么,或者我是否在某处犯了错误。任何帮助都会很棒!

谢谢!

1 个答案:

答案 0 :(得分:0)

我从一个继承自UITextView的类遇到了类似的问题。解决方案结果是我需要覆盖所有init方法。

覆盖init() {super.init()}

覆盖init(frame:CGRect,textContainer:NSTextContainer) {super.init(frame:frame,textContainer:textContainer)}

覆盖init(frame:CGRect) {super.init(frame:frame)}

必需的init(编码器aCoder:NSCoder) {super.init(coder:aCoder)}

相关问题