意外的EXC_BAD_INSTRUCTION

时间:2015-11-11 16:29:51

标签: ios xcode swift

我是swift和iOS的新手!我已经四处寻找在选择单元格后将arrary [indexPath.row]传递给另一个视图的不同方法。由于某种原因,我的工作不正常,我想出了上述错误。我试图更改我的其他视图控制器上的标签,以匹配选定的行,该行匹配自定义表格视图单元格上的标签数组。例如,tableData [0]将是第一行的标签文本。我还从本文中的以下代码中删除了我的其他元素,以专注于segue问题。这是我的viewcontroller:

@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
    }
}

}

1 个答案:

答案 0 :(得分:0)

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

更重要的是,您应该阅读swift tour中关于选项的部分。您正在为self.tableView.indexPathForSelectedRow添加感叹号。这意味着它可能是一个零值,你绝对肯定它不是。

引用Apple:

  

“一旦你确定可选项确实包含一个值,你就可以通过在可选项名称的末尾添加一个感叹号(!)来访问它的基础值。感叹号有效地说:”我知道这个可选肯定有一个值;请使用它。“这被称为强制解包可选的值”