I'm making iOS app which is tableview base.
Here's what I want to do.
Tableview1 -> Tableview2
Once I select one of tableCell, new tableView2 is shown.
I'd like to hilight the cell before new tableView is shown.
Nonetheless, hilight is so slow because of loading new tableView data.
I looked up this. The reason why is prepareForSegue
is called before didSelectRowAtIndexPath
.
Then could you tell me how to call didSelectRowAtIndexPath
before prepareForSegue
?
Or tell me how to set up table cell when cell is selected.
All the best.
What you need is the following function. It will be called before preparedForSegue. First you need to define a var in you class then set it in this function like bellow:
var selectedIndexPath: NSIndexPath = NSIndexPath()
override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
selectedIndexPath = indexPath
return indexPath
}
Then finally you can use this value in preparedForSegue function
You are meshing two different ways to do it.
1 - First Alternative - use only storyboards and prepareForSegue:
Connect your prototype cell to the second viewcontroller in storyboard (the connection must start from the cell and end on the second viewcontroller). In this case, you must use only prepareForSegue method and not didSelectRowAtIndexPath. The second VC is pushed automatically after the prepareForSegue.
2 - Second Alternative - use didSelectRowAtIndexPath:
You use didSelectRowAtIndexPath only if you connect the segue from the first viewcontroller (and not from the cell) to the second viewcontroller. At the end of didSelectRowAtIndexPath method, you must call performSegue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With