3

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?

ACR
  • 185
  • 2
  • 12
  • 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
  • 1
    possible 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 Answers3

11

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
}
Ben Guild
  • 4,881
  • 7
  • 34
  • 60
Bannings
  • 10,376
  • 7
  • 44
  • 54
0

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];
}
Black Sheep
  • 1,665
  • 1
  • 22
  • 32
0

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

MhammedMohie
  • 312
  • 2
  • 9