Reputation: 7569
I want to pass indexPath.row of the clicked row to another controller, so I used the next code, but it prints the error, that view controller does not have a member named tableview.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "body"{
var secondViewController : SecondViewController = segue.destinationViewController as! SecondViewController
var indexPath = self.tableView.indexPathForSelectedRow() //get index of data for selected row
secondViewController.data = indexPath.row // get data by index and pass it to second view controller
}
}
Can I fix this error?
Upvotes: 0
Views: 959
Reputation: 71852
I think the tableView doesn't exists there. and it is not a tableView function.
You have to create an strong outlet for the tableView
like this:
@IBOutlet var tableView: UITableView!
and don't forget to connect this to your tableView
and delete override from the methods TableView.
EDIT:
For your another problem you can follow Kirsteins as he suggested:
tableView.indexPathForSelectedRow
method returns an optional NSIndexPath
. You have to unwrap it. However the best approach would be handle the situation with safely unwrap using if let
where there is no selected row and indexPath
is nil
. Something like:
if let indexPath = tableView.indexPathForSelectedRow() {
secondViewController.tempString = indexPath.row.description
} else {
// handle the situation where the is no selected row
}
and declare a variable into your SecondViewController which will hold this value.
Check THIS for more Info.
Upvotes: 1