Alex
Alex

Reputation: 57

How to access override func tableView property from prepareForSegue method inside same class

I have two viewControllers.First one is a tableViewController and second one is a webViewController.I want to pass data from 1st vc to 2nd vc using segue. For this reason i wants the value of "chosenCellIndex" from override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) to prepareForSegue method. But i can not access the chosenCellIndex value from prepareForSegue method. Please help.

class NewsTableViewController: UITableViewController {

@IBOutlet var Alphacell: UITableView!

var chosenCellIndex = 0
//var temp = 0
var newspaper = ["A","B","C"]


override func viewDidLoad() {
    super.viewDidLoad()

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

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


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Alphacell", forIndexPath: indexPath) as UITableViewCell

    cell.textLabel?.text = newspaper[indexPath.row]

   // Alphacell.tag = indexPath.row
    //Alphacell.addTarget(self, action: "buttonClicked:",forControlEvents: UIControlEvents.TouchUpInside)

    return cell
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    chosenCellIndex = indexPath.row
    //println(chosenCellIndex)

    // Start segue with index of cell clicked
    //self.performSegueWithIdentifier("Alphacell", sender: self)

}


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    let secondViewController = segue.destinationViewController as WebViewController
    secondViewController.receivedCellIndex = chosenCellIndex
    println(chosenCellIndex)
}

}

Upvotes: 2

Views: 370

Answers (3)

In order to get chosen cell index you need to get indexpath for selected row, then you need to get section or row index from indexpath.

See the below code for getting row index

    let indexPath = tableName.indexPathForSelectedRow
    let section = indexPath?.section
    let row = indexPath?.row

Upvotes: 1

Ahmed R.
Ahmed R.

Reputation: 1229

best way to do that is from did select row function, that example

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        let objects = newspaper[indexPath.row]
        let vc = storyboard.instantiateViewControllerWithIdentifier("someViewController") as! UIViewController
        vc.someString = objects.title
        self.presentViewController(vc, animated: true, completion: nil)

}

and prepareForSegue use this code :

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let path = self.tableView.indexPathForSelectedRow()!
    segue.destinationViewController.detail = self.detailForIndexPath(path)
}

Upvotes: 0

rmickeyd
rmickeyd

Reputation: 1551

Make sure you have set the identifier on the segue in storyboard then do:

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "someIdentifier"{
        let secondViewController = segue.destinationViewController as? WebViewController
//Also, make sure this is the name of the ViewController you are perfoming the segue to.
        secondViewController.receivedCellIndex = chosenCellIndex
        print(chosenCellIndex)
    }
    }

Also, what version of Xcode and Swift are you using? println() is now just print().

Upvotes: 0

Related Questions