点击按钮

时间:2016-01-21 20:10:10

标签: xcode uitableview swift2 segue

我有一个带有两列的UITableView:文本colomn和图像

enter image description here

对于左边的textColumn,我使用了标准版,每次都很好......

现在我想实现相同的操作,当点击右键时

我尝试使用右侧(图像)列的新segue imageDisplay

    let tapRecognizerImage = UITapGestureRecognizer(target: self, action: Selector("imageAction:"))
    cell.imageButton.addGestureRecognizer(tapRecognizerImage)

}

func imageAction(sender: UITapGestureRecognizer) {
    performSegueWithIdentifier("imageDisplay", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showDetail" {
        let indexPath:NSIndexPath = self.tableView.indexPathForSelectedRow!
        let controller = segue.destinationViewController as! DetailViewController
        controller.kategorie = self.fetchedResultsController.objectAtIndexPath(indexPath) as! Kategorien
    } else if segue.identifier == "imageDisplay" {
        let indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow!
        let controller = segue.destinationViewController as! DetailViewController
        controller.kategorie = self.fetchedResultsController.objectAtIndexPath(indexPath) as! Kategorien
    }
}

现在在

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

我收到错误

致命错误:在解包可选值时意外发现nil

这是因为self.tableView.indexPathForSelectedRow!没事!

也许,有一个更简单的方法来解决这个问题

1 个答案:

答案 0 :(得分:1)

nil,因为您没有通过点按图片来选择一行 尝试将cell.imageButton.tag设置为indexPath.row(在cellForRowAtIndexPath中)并在sender.tag中使用indexPath代替prepareForSegue。 如果您确实需要NSIndexPath prepareForSegue,只需创建一个并将其行设置为sender.tag