【问题标题】:Change destination viewController when editing is true or false编辑为真或假时更改目标视图控制器
【发布时间】:2018-12-16 10:26:27
【问题描述】:

我有一个viewController和一个tableView,其中包含所有俱乐部,用户补充说。用户可以使用moverowat函数重新排序tableViewCells。如果他选择了一个俱乐部,他会与俱乐部的所有成员一起来到tableView。现在我想实现这个功能,去另一个viewController,在那里他可以编辑单元格的内容。

我已经创建了这个,但现在它不起作用:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if tableViewClub.isEditing == true{
            self.navigationController?.pushViewController(EditClubViewController() as UIViewController, animated: true)}
        if tableViewClub.isEditing == false{
            self.navigationController?.pushViewController(MemberViewController() as UIViewController, animated: true)
        }
 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if !tableViewClub.isEditing {
        guard let destination = segue.destination as? MemberViewController,
             let selectedRow = self.tableViewClub.indexPathForSelectedRow?.row else {
                return
        }

        destination.club = clubs[selectedRow]
        }}

tableView.isEditing 时,会显示带有黑色背景的viewController。当!tableView.isEditing时,会显示这个错误:Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value。我认为这是因为我没有情节提要。但是有可能从tableViewCell 获得两个segues 吗?或者我应该如何解决这个问题?

【问题讨论】:

    标签: swift xcode uitableview segue


    【解决方案1】:

    不要使用 Interface Builder 的 segue。手动实例化目标视图控制器,以便您可以控制要转换到哪一个:

    界面生成器

    选择EditClubViewController 并给它一个故事板ID。对MemberViewController 执行相同操作。

    在代码中

    在第一个视图控制器中:

    override func viewDidLoad() {
        super.viewDidLoad()
        // Turn this on so rows can be tapped during editing mode
        tableView.allowsSelectionDuringEditing = true
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let destinationVC: UIViewController
    
        switch tableView.isEditing {
        case true:
            // If your storyboard isn't named "Main.storyboard" then put the actual name here
            let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "editClubViewController") as! EditClubViewController
            // do your setup....
            destinationVC = vc
        case false:
            let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "memberViewController") as! MemberViewController
            // do your setup...
            destinationVC = vc
        }
        self.navigationController?.pushViewController(destinationVC, animated: true)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-09
      • 2010-09-18
      • 1970-01-01
      相关资源
      最近更新 更多