I have a table view which has cells in it. In each cell there is a button to take you to a new UITableView. I am trying to figure out how to get the index of the cell I clicked the button in from the new UITableView because depending on what cell index I choose it needs to show different items in the new UITableView it takes you to. Any help would be appreciated.
Asked
Active
Viewed 45 times
-1
-
how are you getting to the new tableView? a segue? – Olivier Wilkinson May 11 '16 at 21:28
1 Answers
0
set tag
property of the button equal to the indexPath.row
in cellForRowAtIndexPath
method or in your custom cell class if you are using one.
Hence, when its clicked, you can get the value of indexPath.row
from its tag (sender.tag
).
Note: Assuming there is only one section in your tableview and you wont be inserting or deleting rows runtime. You will have to reload the table in such scenarios.
The best solution that I personally use is, to pass the cell a model object via property or custom method. And later, use the object to make further decisions.

Harshit Gupta
- 1,200
- 12
- 27
-
1This is a very fragile solution that should be avoided. It fails if the table view's rows aren't static. If any rows can be inserted, moved, or deleted, the button's tag can be wrong. – rmaddy May 11 '16 at 21:38
-
totally agree on that. Thank you for pointing it out. I have edited the answer. – Harshit Gupta May 11 '16 at 21:45
-
Would I make the reference to my button in my cell view class or my tabeview class? – Quinn May 11 '16 at 21:50
-
In cell view class. It would be better if you create a modal property in cell class and set it in table view class. – Harshit Gupta May 11 '16 at 21:54
-
-
-
I have it set in my cellForRowAtIndexPath now but I don't know how to call the button.tag from my new tables cell cellForRowAtIndexPath – Quinn May 11 '16 at 22:20
-
create a method in cell class with parameter and call it from cellForRowAtIndexPath – Harshit Gupta May 11 '16 at 22:24