2016-07-07 19 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] 
} 

這工作正常,但我也有兩個賽段來自同一個視圖控制器,以其他兩個VC的。第一個segue(recipeDetail)是當您直接點擊單元格並且工作正常時,但第二個segue(yourSegueIdentifier)是一個按鈕,當您在單元格上激活滑動操作時不會顯示。 Cell Swipe Action

的塞格斯:

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是走出來的零,並給予以下錯誤消息 error on line 13

+0

indexPathForSelectedRow將返回零,可能是因爲你的行不處於選中狀態。你在桌子視圖上選擇了嗎?你是否實現了'didSelectRowAtIndexPath'委託方法?滑動行不會自動將其置於選定狀態。 – SArnab

回答

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

我應該如何設置swipeIndex變量? – zach2161

+0

在你的類ViewController下:UIViewController。只需添加:var swipeIndex:Int! –

+0

編輯答案更好地解釋:) –

相關問題