Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go to the ViewController by clicking on the TableView cell in swift

Tags:

xcode

ios

swift

there is a class MenuViewController in which the array recorded in the table is recorded:

import Foundation
import UIKit

class MenuViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var menuTableView: UITableView!

let myTitle = ["Помощь", "Информация", "Поддержка"]


override func viewDidLoad() {
    super.viewDidLoad()


    menuTableView.delegate = self
    menuTableView.dataSource = self
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = menuTableView.dequeueReusableCell(withIdentifier: "MenuCell") as! MenuTableViewCell
    cell.labelText.text = myTitle[indexPath.row]
    return cell
}
}

What do you need to write in it to go to Info Controller?

like image 737
aaallleeexxx918 Avatar asked Dec 14 '17 18:12

aaallleeexxx918


People also ask

How do you perform a segue in a tableView cell?

For this, we are going to select the “show” segue in the “Selection Segue” section. This means that when you tap on the cell, it will cause this segue to happen. If you use the “Accessory Action” one, that will be what happens if they tap on the accessory of the UITableViewCell.

How to display a cell from a second view controller?

Click on it, go to its attribute inspector and give it “cell” as Identifier. Now from the prototype cell, press control and drag to the Second View Controller and select show from there.

How to create a segue from a uitableviewcell to another viewcontroller?

To create a segue from a UITableViewCell to another View controller, we’ll do it like any other ViewController to ViewController segue. We’ll do this with help of an example here. First Create a project, delete the View Controller from storyboard and add One Table View Controller and one View Controller in the storyboard.

How to create a table view in Xcode?

Open your Main.storyboard file, click on the + button on top right corner of your XCode. It will show you a list of items. Search for Table View, drag and drop it on ViewController screen.

How to drag cells from one view controller to another view controller?

For other users coming to this question, if you have a static cells in a UITableViewController, you can just control drag from the cell to the new View Controller. You can also do this on dynamic cells if you don't need to pass any data to the next View Controller.


2 Answers

I assume you are using storyboards. First you need to set up some sort of connection between your two viewcontrollers. You can do this by setting up a "segue". You hold ctrl+click drag from the first viewcontroller to the 2nd viewcontroller. It looks like this:

Creating Segue

When you let go of the mouse, you will see this:

Creating Segue 2

Click on Show. Now you will have created a segue. Now you need to name it. So click on the segue that shows up in the storyboard. It is basically just an arrow from the Menu View Controller to the Info View Controller. On the right hand side you will see a place where you can name it (give it an identifier):

Creating Segue 3

Now ... you have done all you needed to do in the storyboard. Now in your actual code in the MenuViewController, you need to associate clicking the tableviewcell. You do this by using the delegate method didSelectRowAt.

I see you have already set up the delegate with this line: menuTableView.delegate = self

That ensures that didSelectRowAt will be called whenever the user taps on a row.

So now what you want to do is write the code to perform the segue:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.performSegue(withIdentifier: "YourSegueName", sender: self)
}

Note: Here is more information on interacting with a tableview row: How to detect tableView cell touched or clicked in swift

Here is more information about segues: IOS - How to segue programmatically using swift

There are many more customizations you can do ... such as passing data through to the next viewcontroller via the segues. Here's how: Pass data through segue

like image 147
Gabriel Pires Avatar answered Nov 15 '22 04:11

Gabriel Pires


implement UITableViewDelegate method:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

       //To InfoViewController I am considering you have created InfoViewController with XIB 
       let controller = InfoViewController(nibName: "YourNibName", bundle: nil)

        //Present Your Controller
        self.present(controller, animated: true) {
        }              

        //Push Your controller if your view is already part of NavigationController stack
        self.navigationController?.pushViewController(controller, animated: true)
    }
like image 26
Jeetendra Kumar Avatar answered Nov 15 '22 04:11

Jeetendra Kumar