【问题标题】:Bad Exception on swipe action cell Swift滑动动作单元格Swift上的错误异常
【发布时间】:2016-07-07 20:06:34
【问题描述】:

我正在制作一个应用程序,它使用滑动操作将单元格内的文本信息发送到 WebViewController。滑动动作是:

let sendToWebsite = UITableViewRowAction(style: .Default, title: "Website")
{ (action, indexPath) in
    self.performSegueWithIdentifier("yourSegueIdentifier", sender: nil)
}
    sendToWebsite.backgroundColor = UIColor.blueColor()
    return [sendToWebsite]
}

这很好用,但我也有两个 segues 来自同一个视图控制器,到另外两个 VC。第一个 segue(recipeDetail) 是当您直接点击单元格并且工作正常时,但第二个 segue(yourSegueIdentifier) 是一个按钮,当您激活单元格上的滑动操作时会出现并且不起作用。

转场:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (segue.identifier == "recipeDetail") {
        let indexPath = self.tableView!.indexPathForSelectedRow
        let destinationViewController: DetailViewController = segue.destinationViewController as! DetailViewController

        destinationViewController.recipe = recipes[indexPath!.row]
    }
    else if segue.identifier == "yourSegueIdentifier"
    {
        let indexPath = self.tableView!.indexPathForSelectedRow
        let nextVC: WebViewController = segue.destinationViewController as! WebViewController

        nextVC.recipe = recipes[indexPath!.row]
    }
}

上线

nextVC.recipe = recipes[indexPath!.row]

indexPath 显示为 nil 并给出以下错误消息

【问题讨论】:

  • indexPathForSelectedRow 返回 nil,可能是因为您的行未处于选中状态。您是否在表格视图上启用了选择?您是否实现了didSelectRowAtIndexPath 委托方法?滑动该行不会自动将其置于选定状态。

标签: swift uitableview null segue exc-bad-instruction


【解决方案1】:

好吧,看起来在滑动操作上,tableView 没有注册“indexPathForSelectedRow”方法。 您也可以设置一个全局 swipeIndex 变量

class ViewController: UIViewController{

var swipeIndex : Int!
//Code, code, code...

然后在调用滑动操作后设置它。

let sendToWebsite = UITableViewRowAction(style: .Default, title: "Website")
{ (action, indexPath) in
    self.swipeIndex = indexPath.row
    self.performSegueWithIdentifier("yourSegueIdentifier", sender: nil)
}
    sendToWebsite.backgroundColor = UIColor.blueColor()
    return [sendToWebsite]
}

然后:

else if segue.identifier == "yourSegueIdentifier"
    {
        let indexPath = self.tableView!.indexPathForSelectedRow
        let nextVC: WebViewController = segue.destinationViewController as! WebViewController

        nextVC.recipe = recipies[self.swipeIndex]
    }
}

【讨论】:

  • 我应该如何设置 swipeIndex 变量?
  • 就在你的类 ViewController 下:UIViewController。只需添加: var swipeIndex : Int!
  • 编辑了答案以便更好地解释:)
  • 谢谢!还有一件事,在最后一行,nextVC.recipe = self.swipeIndex,有一个错误说“无法分配'Int!'类型的值!输入“食谱?””
  • 哈哈,更新了我的答案。如果这有帮助,请投票/标记为正确答案:)
猜你喜欢
  • 1970-01-01
  • 2016-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多