我有一个带有单元格的表视图。当我按下单元格时它没有显示第二个视图控制器,当我按下另一个单元格时,它给了我这个错误:
致命错误:在展开Optional值时意外发现nil (LLDB)
这是我的代码:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "toMediaPlayer" {
let VC = segue.destinationViewController as! UIViewController
let indexPath: NSIndexPath = tableView.indexPathForSelectedRow()!
VC.title = songs[indexPath.row]
}
答案 0 :(得分:0)
我建议使用indexPathForCell()
而不是indexPathForSelectedRow()
来获取索引路径。相应的单元格作为sender
参数
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "toMediaPlayer" {
if sender is UITableViewCell {
let VC = segue.destinationViewController as! UIViewController
let indexPath = tableView.indexPathForCell(sender)
VC.title = songs[indexPath.row]
}
}
我怀疑错误发生在获取destinationViewController的行中