Search code examples
swiftxcodeuitableviewsegue

Change destination viewController when editing is true or false


I have a viewControllerwith a tableViewwhich contains all clubs, the user added. The user can reorder the tableViewCellswith the moverowatfunction. If he selects a club, he comes to a tableViewwith all members from the club. Now I want to implement the ability, to get to another viewController where he can edit the content of the cell.

I have created this, but now it doesn't work:

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

When tableView.isEditing, then a viewController with a black background is shown. When !tableView.isEditing, this error is shown:Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value. I think it is because I have no storyboard segues. But is it possible, to have two segues from the tableViewCell? Or how should I solve this problem?


Solution

  • Don't use Interface Builder's segue. Instantiate the destination view controller manually so you can control which one you are transitioning to:

    Interface Builder

    Select the EditClubViewController and give it a Storyboard ID. Do the same to MemberViewController.

    enter image description here

    In code

    In the first view controller:

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