ios - 意外的 EXC_BAD_INSTRUCTION

标签 ios xcode swift

我是 swift 和 iOS 新手!我四处寻找在选择单元格后将 arrary[indexPath.row] 传递到另一个 View 的不同方法。由于某种原因,我的无法正常工作,并且出现了上述错误。我正在尝试更改其他 View Controller 上的标签,以匹配与自定义 TableView 单元格上的标签数组相匹配的选定行。例如,tableData[0] 将是第一行上的标签文本。我还从本文中的以下代码中删除了其他元素,以重点关注转场问题。这是我的 View Controller :

@IBOutlet weak var tableView: UITableView!


var tableData: [String] = ["8:00 am", "8:10 am", "8:20 am", "8:30 am", "8:40 am", "8:50 am", "9:00 am", "9:10 am", "9:20 am", "9:30 am", "9:40 am", "9:50 am", "10:00 am", "10:10 am", "10:20 am", "10:30 am", "10:40 am", "10:50 am", "11:00 am", "11:10 am", "11:20 am", "11:30 am", "11:40 am", "11:50 am", "12:00 pm", "12:10 pm", "12:20 pm", "12:30 pm", "12:40 pm", "12:50 pm", "1:00 pm", "1:10 pm", "1:20 pm", "1:30 pm", "1:40 pm", "1:50 pm", "2:00 pm", "2:10 pm", "2:20 pm", "2:30 pm", "2:40 pm", "2:50 pm", "3:00 pm", "3:10 pm", "3:20 pm", "3:30 pm", "3:40 pm", "3:50 pm", "4:00 pm", "4:10 pm", "4:20 pm", "4:30 pm", "4:40 pm", "4:50 pm", "5:00 pm"]


let conSegueIdentifier = "ShowConSegue"
var timeToPass:String!

override func viewDidLoad() {
    super.viewDidLoad()

    let nib = UINib(nibName: "vwTblCell", bundle: nil)
    tableView.registerNib(nib, forCellReuseIdentifier: "cell")

    coursePicker.hidden = true;
    self.courseTextfield.delegate = self

    playerPicker.hidden = true;
    self.playerTextfield.delegate = self

    timePicker.hidden = true;
    self.timeTextfield.delegate = self

    datePicker.hidden = true;
    self.dateTextfield.delegate = self
    datePicker.addTarget(self, action: Selector("datePickerChanged:"), forControlEvents: UIControlEvents.ValueChanged)
}

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

//tableview

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    var numRows = tableData.count
    if(timeTextfield.text == timeData[1]) {
        numRows = 25
    }
    else if(timeTextfield.text == timeData[2]) {
        numRows = 30
    }
    return numRows
}

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

    let cell:TblCell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TblCell

    if(timeTextfield.text == timeData[2]) {
        cell.lblTime.text = tableData[indexPath.row + 25]
        cell.lblOne.text = lblOneData[indexPath.row + 25]
    }
    else {
        cell.lblTime.text = tableData[indexPath.row]
        cell.lblOne.text = lblOneData[indexPath.row]
    }

    cell.lblTwo.text = "Available"
    cell.lblThree.text = "Available"
    cell.lblFour.text = "Available"

    return cell
}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 120
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: true)

    let row = indexPath.row

    performSegueWithIdentifier(conSegueIdentifier, sender: self)
    print(tableData[row])
}

//end of tableview

//segue
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == conSegueIdentifier {
        let cvc = segue.destinationViewController as! ConViewController;
        let indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow!
        let valueToPass = tableData[indexPath.row]
        cvc.conName = courseTextfield.text
        cvc.conDateName = dateTextfield.text
        cvc.conTimeName = valueToPass
    }
}

}

最佳答案

注意你做了什么:
在 didSelectRowAtIndexPath 中,您获取了选定的行并取消选择了该行。
然后,您调用prepareForSegue并向tableView询问所选行,但现在没有。该函数返回 nil
如果您删除“deselectRowAtIndexPath”,它应该可以工作。

更重要的是,您应该阅读 swift tour 中有关可选的部分。 。您正在将感叹号附加到 self.tableView.indexPathForSelectedRow。这意味着它可能是一个零值,并且您绝对确定它不是。

引用苹果的话:

"Once you’re sure that the optional does contain a value, you can access its underlying value by adding an exclamation mark (!) to the end of the optional’s name. The exclamation mark effectively says, “I know that this optional definitely has a value; please use it.” This is known as forced unwrapping of the optional’s value"

关于ios - 意外的 EXC_BAD_INSTRUCTION,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33655168/

相关文章:

ios - 点击扫描平面时如何将 3D 对象添加到引用图像?

swift - Xcode9 是否向后兼容 Swift 3(或 3.1)?

html - iFrame 和 Div ID 无法按预期用于 iOS iFrame 修复

c++ - 使用 libsodium、randombytes 错误为 iOS 构建 zeromq

swift - 在 firebase 中将电子邮件域列入白名单

xcode - 将 Swift 中 HTTP 请求返回的 NSData 转换为字符串

json - JSON 中的值可以是数组和对象

ios - 屏幕尺寸减去广告尺寸

ios - 为什么我的图像有时无法在模拟器中显示?

ios - 如何在 ViewController 顶部设置标题