gpt4 book ai didi

ios - 意外的 EXC_BAD_INSTRUCTION

转载 作者:行者123 更新时间:2023-11-30 13:55:39 27 4
gpt4 key购买 nike

我是 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com