Ios 主详细信息应用程序indexPathForSelectedRow问题
我被卡住了。我遇到了一个棘手的问题,让我的自定义表视图单元格转到我的主详细信息应用程序的“详细信息”部分,并通过调用prepareForSegue函数对其进行了修复。我仍然没有得到我所期望的结果,我发现了一个大问题:将单元格的属性传递到细节部分需要选择一个单元格,而该帖子中的代码在紧接着segue之前取消选择该单元格。这似乎是一个简单的解决办法,但当我删除这一行时,当我点击一个单元格时,程序崩溃了。我真的不知道在这里该做什么(我100%的iOS体验来自过去48小时),我希望这里的人能帮上忙 涉及的代码:Ios 主详细信息应用程序indexPathForSelectedRow问题,ios,swift,uitableview,tableview,Ios,Swift,Uitableview,Tableview,我被卡住了。我遇到了一个棘手的问题,让我的自定义表视图单元格转到我的主详细信息应用程序的“详细信息”部分,并通过调用prepareForSegue函数对其进行了修复。我仍然没有得到我所期望的结果,我发现了一个大问题:将单元格的属性传递到细节部分需要选择一个单元格,而该帖子中的代码在紧接着segue之前取消选择该单元格。这似乎是一个简单的解决办法,但当我删除这一行时,当我点击一个单元格时,程序崩溃了。我真的不知道在这里该做什么(我100%的iOS体验来自过去48小时),我希望这里的人能帮上忙 涉及
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetail" {
if let indexPath = self.tableView.indexPathForSelectedRow {
let object = objects[indexPath.row]
let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController
controller.detailItem = object
controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()
controller.navigationItem.leftItemsSupplementBackButton = true
}
}
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath)
tableView.deselectRowAtIndexPath(indexPath, animated: false) // What appears to be the problem
performSegueWithIdentifier("showDetail", sender: cell)
}
错误(删除取消选择行时):
(在编辑器中会弹出:线程1:EXC_断点)
非常感谢您的帮助。您可以尝试这样放置代码:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath)
performSegueWithIdentifier("showDetail", sender: cell)
tableView.deselectRowAtIndexPath(indexPath, animated: false) // What appears to be the problem
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let object = objects[indexPath.row]
performSegueWithIdentifier("showDetail", sender: object)
tableView.deselectRowAtIndexPath(indexPath, animated: false)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetail" {
if let object = sender as! "type of object" {
let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController
controller.detailItem = object
controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()
controller.navigationItem.leftItemsSupplementBackButton = true
}
}
}
但我认为最好是在didSelectRowAtIndexpath
中将要发送到下一屏幕的对象,并将其传递给performsgue
,如下所示:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath)
performSegueWithIdentifier("showDetail", sender: cell)
tableView.deselectRowAtIndexPath(indexPath, animated: false) // What appears to be the problem
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let object = objects[indexPath.row]
performSegueWithIdentifier("showDetail", sender: object)
tableView.deselectRowAtIndexPath(indexPath, animated: false)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetail" {
if let object = sender as! "type of object" {
let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController
controller.detailItem = object
controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()
controller.navigationItem.leftItemsSupplementBackButton = true
}
}
}
希望这有帮助 如果segue连接到表视图单元格(而不是控制器),则会自动调用
prepareforsgue
。您可以在didselectrowatinexpath
中取消选择单元格,但不能显式调用performsguewithidentifier
。