I have a UITableView set up with custom cells in it. Each cell has a switch in it and I need to get the indexPath of a specific cell every time a switch is interacted with. How can I return the indexPath of a cell from a UITableView with custom cells each time a switch is interacted?
-
Did you create your cell programmatically, or through IB? – Sergey Kalinichenko Jun 26 '15 at 01:45
-
@dasblinkenlight The cells were created making an .xib file and linking it to a subclass of UITableViewCell where I set everything up, and then I used the tableview methods to populate the table with cells (not sure if this is the answer you were looking for so let me know) – ACR Jun 26 '15 at 01:49
-
If you have a single section, you could set the `Tag` property of the switch to the row of the index path when you populate the table with cells. – Sergey Kalinichenko Jun 26 '15 at 01:52
-
1possible duplicate of [Detecting which UIButton was pressed in a UITableView](http://stackoverflow.com/questions/1802707/detecting-which-uibutton-was-pressed-in-a-uitableview) – Kevin Jun 26 '15 at 01:58
-
possible duplicate of [Get button click inside UI table view cell](http://stackoverflow.com/questions/20655060/get-button-click-inside-ui-table-view-cell) – kRiZ Jun 26 '15 at 02:03
3 Answers
You can use the indexPathForRowAtPoint
like:
@IBAction func switchChanged(sender: UIControl) {
let rowPoint = sender.convertPoint(sender.bounds.origin, toView: self.tableView)
let indexPath = self.tableView.indexPathForRowAtPoint(rowPoint)
println(indexPath)
}
This is the Objective C version
- (IBAction)toggleSwitch:(id)sender {
UISwitch *switchInCell = (UISwitch *)sender;
CGPoint pos = [switchInCell convertPoint:switch.bounds.origin toView:tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:pos];
}
If you want to get the cell's indexPath in your custom cell:
// CustomCell.swift
@IBAction func switchChanged(sender: UIControl) {
let tableView = self.tableView()
let indexPath = tableView?.indexPathForCell(self)
println(indexPath)
}
func tableView() -> UITableView? {
var tableView = self.superview
while (tableView != nil && !(tableView is UITableView)) {
tableView = tableView!.superview
}
return tableView as? UITableView
}
-
-
Since my tableView is in a different class than my custom cell's class, how do I deal with the `toView: self.tableView` in `sender.convertPoint`? – ACR Jun 26 '15 at 02:17
-
-
I want to get the indexPath of the cell in the tableView. This is because in my app I have a tableView of 8 cells which corresponds to the 8 switches in the tableView. – ACR Jun 26 '15 at 02:28
-
-
-
-
-
Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/81650/discussion-between-bannings-and-acr). – Bannings Jun 26 '15 at 14:52
One way I can think to solve this is
EDIT This might work in iOS6 or older
- (void)toggleSwitch:(id)sender {
UISwitch *switch = (UISwitch *)sender;
UITableViewCell * cell = (UITableViewCell*) switch.superview;
NSIndexPath *indexpath = [[self yourTableView] indexPathForCell:cell];
}

- 1,665
- 1
- 22
- 32
-
This is dangerous. Different versions of iOS can have differing numbers of views between the sender and the cell. – Bannings Jun 26 '15 at 01:59
-
@Bannings True, although could you not loop through its superviews and look for the UITableViewCell? – Jun 26 '15 at 02:07
-
One section TableView:
in the method cellForRowAtIndexPath
you can set the tag of the current cell's switch something like cell.switch.tag = indexPath.Row
and in your toggling method you can get the tag of your switch
- (void)toggleSwitch:(id)sender {
UISwitch *switch = (UISwitch *)sender;
int row = switch.tag;
//you can switch on the switch values to to get the exact switch then do your action
}
Multi sections tableView:
Make a custom switch class that have a property NSIndexPath
then in the method cellForRowAtIndexPath
you can set this property with the actual cell's indexpath, but don't forget to reset this property in prepareForReuse
method in the custom cell.
and in your toggling method you will get the actual indexPath

- 312
- 2
- 9