ios - 主详细信息应用程序indexPathForSelectedRow问题

标签 ios swift uitableview tableview

好吧,我被困住了。我在让自定义表格 View 单元格转到我的主从应用程序的“详细信息”部分时遇到了一个棘手的问题,并且 this other StackOverflow post修复了它,调用prepareForSegue函数。我仍然没有得到我期望的结果,并且我发现了一个大问题:将单元格的属性传递到详细信息部分需要选择一个单元格,并且该帖子中的代码会在转场之前立即取消选择该单元格。似乎是一个简单的修复,但是当我删除该行时,当我单击一个单元格时程序崩溃了。我真的不知道该怎么办(我的 iOS 体验 100% 来自过去 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)
}

错误(当我删除取消选择行时):

fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

(在编辑器中会弹出:THREAD 1: EXC_BREAKPOINT)

感谢所有帮助。

最佳答案

你尝试输入这样的代码:

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
}

但我认为更好的是,您应该在 didSelectRowAtIndexpath 中获取要发送到下一个屏幕的对象,并将其传递给 performSegue,如下所示:

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

希望这有帮助!

关于ios - 主详细信息应用程序indexPathForSelectedRow问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34457957/

相关文章:

ios - Square 注册 IOS SDK 的用户 ID 不匹配

swift - 如何从另一个类的确切实例引用属性?

swift - Swift 中对象声明的区别

ios - 遍历 UITableViewCells 的奇怪行为

ios - 无法在 IOS7 中重新加载 TableView

iphone - 我如何在 app-info.plist 中使用常量,这样我就不会硬编码了?

ios - 当添加到 UICollectionView 时,layoutSubviews 会卡住应用程序

ios - iOS 的打包和 AIR 应用程序导致版本号差异

ios - MKMapView 中的动画标题更改

ios - 将界面生成器中的 tableView 连接到 NON-outlet 变量