Search code examples
iosswiftuitableviewseguecell

How can i use uitableview cell for performing segue


I started to learn Swift 2 weeks ago. With some courses and youtube videos, I learn basics of uitableview but I couldn't get how can I use specific table view cell like a button.

What I wanna do?

I am trying to use the sidebar menu(https://github.com/yannickl/FlowingMenu) for my project, everything is cool and working okay instead of using cells.

In every basic tutorial people showing how to use segue with uitableview but they only use 2 pages and independent from cell value, no matter what text in your cell it calls segue for the second controller.

I want to use the sidebar menu with cells and if my cell value is "main menu" I want to perform "toMainMenu" segue.

I only know using cells with arrays don't know is there any other way for work with cells, and my main goal is cell-specific segue.

I watched a lot of tutorials but can't find anything useful for me.

import UIKit

import FlowingMenu

class SideBarMenu: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var surnameLabel: UILabel!
@IBOutlet weak var topBar: UINavigationBar!
@IBOutlet weak var backButtonItem: UIBarButtonItem!
@IBOutlet weak var userTableView: UITableView!
@IBOutlet weak var profilePic: UIImageView!
let CellName  = "Menu"
let mainColor = UIColor(hex: 0xC4ABAA)
var sections = ["Main Menu", "Settings", "Profile"]
override func viewDidLoad() {
    super.viewDidLoad()
    userTableView.delegate = self
    userTableView.dataSource = self
    nameLabel.text = Helper.name
    surnameLabel.text = Helper.surname

    topBar.tintColor              = .black
    topBar.barTintColor           = mainColor
    topBar.titleTextAttributes    = [
        NSAttributedString.Key.font: UIFont(name: "HelveticaNeue-Light", size: 22)!,
        NSAttributedString.Key.foregroundColor: UIColor.black
    ]
    userTableView.backgroundColor = mainColor
    view.backgroundColor          = mainColor
}



// MARK: - Managing the Status Bar

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

// MARK: - UITableView DataSource Methods

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return sections.count
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell()
    cell.textLabel?.text = sections[indexPath.row]
    cell.contentView.backgroundColor = mainColor
    return cell
}

// MARK: - UITableView Delegate Methods

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("Select row at")
}
@IBAction func profileButton(_ sender: Any) {
    performSegue(withIdentifier: "toProfile", sender: nil)
}

}

I want to perform "toSettings" segue when "Settings" cell clicked.


Solution

  • Implement the UITableViewDelegate protocol method tableView(_:didSelectRowAt:) as below

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
       switch sections[indexPath.row] {
       case "Main Menu":
          performSegue(with: "toMainMenu", sender: nil)
       case "Settings":
          performSegue(with: "toSettings", sender: nil)
       case "Profile":
          performSegue(with: "toProfile", sender: nil)
       }
    }
    

    If you need to configure or pass some data to the destination controller, you can override the controller's prepare(for:sender:) method to get a reference, for example:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
       if segue.identifier == "toMainMenu" {
          let dvc = segue.destination as! MenuViewController
          // now you have a reference
       }
    }