TableView单元格,swipeactions和不同的段落Swift 4

时间:2018-01-19 19:35:50

标签: swift swift4

在ViewController A中,我有一个带有两个trailingSwipeActions和一个leadingSwipeAction的TableView。如果你点击单元格有一个segue到ViewController B. trailingSwipeActions调用ActionSheets,在ViewController A中处理.situe是在'prepare for segue'函数中执行的。这一切都很有效。

现在我想为你执行leadingSwipeAction添加另一个segue。那个segue必须导致ViewController C.我不能在Storyboard中添加另一个segue,从customcell到ViewController C ......然后当我点击单元格时......只有一个segue被调用(而不是另一个) 。那么......该怎么做。

以下是我的代码的相关部分:

override func tableView(_ tableView: UITableView,
                        leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
{
    let weightAction = UIContextualAction(style: .normal, title:  "Gewicht", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
        success(true)
    })
    weightAction.image = UIImage(named: "Scale")
    weightAction.backgroundColor = UIColor(red: 0/255, green: 122/255, blue: 255/255, alpha: 1)
    return UISwipeActionsConfiguration(actions: [weightAction])
}

我想要的是:如果点击名为“Scale”的图标,则会触发到ViewController C的segue。

另外我的'准备segue'函数的代码(对于segue到ViewController B):

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "detailSegue"{
        let rowClicked = (self.tableView.indexPathForSelectedRow?.row)!
        let destVC = segue.destination as! UINavigationController
        let slideVC = destVC.topViewController as! detailViewController
        slideVC.araKuiken = araKuikens[rowClicked]
    }
}

那么如何在ViewController C中添加一个segue,当我点击“缩放”按钮时会调用它?

2 个答案:

答案 0 :(得分:1)

如果你想让你的weightAction触发一个segue到viewController C,你只需要在处理程序块中执行那个segue就像这样:

let weightAction = UIContextualAction(style: .normal, title:  "Gewicht", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
    self.performSegue(withIdentifier: "viewControllerC", sender: ac)
    success(true)
})

然后,在您的prepare代码中,您也想要处理这种可能性:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  if segue.identifier == "detailSegue"{
    let rowClicked = (self.tableView.indexPathForSelectedRow?.row)!
    let destVC = segue.destination as! UINavigationController
    let slideVC = destVC.topViewController as! detailViewController
    slideVC.araKuiken = araKuikens[rowClicked]
  } else if segue.identifier == "viewControllerC" {
    //Perform your preparation code here
  }
}

当然,你仍然需要在Storyboard中创建segue,如你所提到的那样,通过从viewControllerA顶部的黄色圆圈拖动到viewControllerC,并给它正确的名称(我使用“viewControllerC”作为例子)。

答案 1 :(得分:0)

当单元格显示侧面菜单LeadingSwipeActionsConfigurationForRowAt()时,我将indexPath保存在一个变量中

然后我在Prepare()方法中使用它

    if let indexPath = self.indexPathSelected {
        if segue.identifier == "IdentifierA" {

        }else if segue.identifier == "IdentifierB" {
            let destino          = segue.destination as! VCdestinarion
            destino.intLibro     = Int(self.arrData[indexPath.row][1]
        }


    }

这对我有用。

致谢