2016-07-07 3 views
0

スワイプアクションを使用して、セル内のテキストに関する情報をWebViewControllerに送信するアプリケーションを作成しています。スワイプのアクションは次のとおりです。スワイプアクションセルで例外が発生しないSwift

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

これはうまくいきますが、同じView Controllerから2つの他のVCにも2つのセグがあります。最初のsegue(recipeDetail)は、セルを直接タップしてうまく動作するときですが、セカンドセグ(yourSegueIdentifier)はセル上でスライドアクションをアクティブにしても動作しないときに表示されるボタンです。 Cell Swipe Action

segues:

nextVC.recipe = recipes[indexPath!.row] 

indexPathがnilとして出てくると error on line 13

+0

indexPathForSelectedRowは、nilを返しています。テーブルビューで選択を有効にしましたか? 'didSelectRowAtIndexPath'デリゲートメソッドを実装しましたか?行をスワイプしても自動的には選択された状態にはなりません。 – SArnab

答えて

1

は、まあ、それはスワイプのように見える次のエラーメッセージを与えている行で

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

tableViewは "indexPathForSelectedRow"メソッドを登録しません。 何を交互に可能性は、セットアップが

class ViewController: UIViewController{ 

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

変数、その後スワイプアクションが呼ばれたら、それを設定するグローバルswipeIndexです。

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] 
    } 
} 
+0

swipeIndex変数の設定方法を教えてください。 – zach2161

+0

ViewController:UIViewControllerクラスのすぐ下にあります。追加するだけです:var swipeIndex:Int! –

+0

もっとよく説明するために答えを編集しました:) –

関連する問題