Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call didSelectRowAtIndexPath before prepareForSegue?

Tags:

ios

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.

like image 745
Noriaki Takamizawa Avatar asked Nov 05 '12 15:11

Noriaki Takamizawa


3 Answers

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

like image 125
Hamid Avatar answered Jan 02 '23 00:01

Hamid


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.

like image 40
LombaX Avatar answered Jan 02 '23 00:01

LombaX


  1. Make segue for controller->controler
  2. run data loading in didselectrowatindexpath
  3. When it done -> perform your segue
like image 23
NeverBe Avatar answered Jan 02 '23 02:01

NeverBe