ACR
ACR

Reputation: 185

UITableViewCell Segue not Working

I'm making an app where I have a UITableView that the user adds data to, and I want the user to be able to tap on the cell and see more details about the cell they chose. I set up a segue from the cell to my cellDetailController where I will later add all the details of the cell they chose. When I run the app and try to tap on the cell to segue, it just selects the cell and nothing happens. What am I doing wrong here? Here is my storyboard set up:

(I cant upload pictures yet so here's a link) http://tinypic.com/r/2ivcwsj/8

Heres the code for my the class connected to my tableView:

import UIKit

typealias eventTuple = (name: String, date: String)

var eventList : [eventTuple] = []

    class TableViewController: UIViewController, UITableViewDelegate {

        @IBOutlet weak var eventTable: UITableView!

        override func viewDidLoad() {
            super.viewDidLoad()

        }

        override func viewDidAppear(animated: Bool) {
            eventTable.reloadData()


        }

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

        // MARK: - Table view data source



        func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            // #warning Incomplete method implementation.
            // Return the number of rows in the section.
            return eventList.count
        }


        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")

            cell.textLabel?.text = eventList[indexPath.row].name
            cell.detailTextLabel?.text = eventList[indexPath.row].date

            return cell
        }



        /*
        // Override to support editing the table view.
        override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
            if editingStyle == .Delete {
                // Delete the row from the data source
                tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
            } else if editingStyle == .Insert {
                // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
            }    
        }
        */

        /*
        // Override to support rearranging the table view.
        override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {

        }
        */

        /*
        // Override to support conditional rearranging of the table view.
        override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
            // Return NO if you do not want the item to be re-orderable.
            return true
        }
        */


        // MARK: - Navigation

        // In a storyboard-based application, you will often want to do a little preparation before navigation
        override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
            if(segue.identifier == "showDetailSegue") {

            }     
        }
    }

Upvotes: 2

Views: 5122

Answers (2)

blackwatch
blackwatch

Reputation: 1

It should not be necessary to override didSelectRowAtIndexPath to achieve this functionality. See Apple's Working with Table Views. I ran into this same problem but it was due to an error in the TableViewController. The culprit was in one of the tableView methods:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell`

If you're using a custom UITableViewCell class, then tableView() should return an instance of your custom class. In my case, I forgot to downcast the value returned from dequeueReusableCell() within this method. The call to dequeueReusableCell() should look something like this:

guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? CustomTableViewCell  else {
    fatalError("The dequeued cell is not an instance of CustomTableViewCell.")
}

Upvotes: 0

zisoft
zisoft

Reputation: 23078

Drag the segue from the tableViewController (the yellow icon on the top of the viewController), not from the tableCell. Then give that segue an identifier.

Override didSelectRowAtIndexPath and perform the segue here via performSegueWithIdentifier()

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    // pass any object as parameter, i.e. the tapped row
    performSegueWithIdentifier("showDetailSegue", sender: indexPath.row)
}

Upvotes: 8

Related Questions