user5630104
user5630104

Reputation:

Push a segue at selected row swift 3

I am trying to push a segue after having selected a row. I have tried various ways through other peoples questions but I am getting no where. Here is my code:

import UIKit
import CoreData

class RoutineViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var tableview: UITableView! {
    didSet {
        tableview.dataSource = self
    }
}

var routines: [NSManagedObject] = []

var stringToPass:String!

override func viewDidLoad() {

    super.viewDidLoad()

    tableview.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")

}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
        return
    }

    let managedContext = appDelegate.persistentContainer.viewContext

    let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Routine")

    do {
        routines = try managedContext.fetch(fetchRequest)
    } catch let error as NSError {
        print("Could not fetch. \(error), \(error.userInfo)")
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return routines.count


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

    let cell = tableview.cellForRow(at: indexPath)
    tableview.deselectRow(at: indexPath, animated: true)

    performSegue(withIdentifier: "R2", sender: cell)

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        if segue.identifier == "R2" {

            self.performSegue(withIdentifier: "R2", sender: self)
        }

    }

I do have the table working and everything, there is a save function, delete function and it fetches and updates I just cannot get it to segue to another view controller. The segue identifier is called "R2".

Upvotes: 0

Views: 612

Answers (3)

nayem
nayem

Reputation: 7585

You haven't set tableview.delegate to self.

tableView(_:didSelectRowAt) is the Table View Delegate method. Somewhere set this in your viewDidLoad.

override func viewDidLoad() {
    ...
    tableview.delegate = self
    ...
}

Upvotes: 1

Sakir Sherasiya
Sakir Sherasiya

Reputation: 1572

For monitor any activity in UITableView give delegate as self.

@IBOutlet weak var tableview: UITableView! {
       didSet { 
            tableview.dataSource = self
            tableview.delegate = self
            }
 }

Upvotes: 0

Sour LeangChhean
Sour LeangChhean

Reputation: 7409

Try change to this:

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

let cell = tableview.cellForRow(at: indexPath)
tableview.deselectRow(at: indexPath, animated: true)

performSegue(withIdentifier: "R2", sender: self)

}


override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "R2" {


    }

}

Upvotes: 0

Related Questions