Retrieve row table id when i click switch?

I have a table view with a switch control. My problem: how can I get the row table id when I click the switch? I can get switch state, but not id

This is my code to get the switching state:

- (void)aggiungiTag:(id)sender { NSLog(@"the tag value is: %d", [sender isOn]); return; } 

This is my code for controlling a push button switch in a cell:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; cell.selectionStyle = UITableViewCellSelectionStyleNone; } // Configure the cell... //inseriamo nelle celle la nostra lista cell.textLabel.text = [arrTagResidui objectAtIndex:indexPath.row]; /*** aggiungo lo switch per i tag ***/ //lo istanzio e setto la posizione UISwitch *switchObj = [[UISwitch alloc] initWithFrame:CGRectMake(1.0, 1.0, 20.0, 20.0)]; //setto il valore di default switchObj.on = NO; //setto l'action ed i controlli degli eventi [switchObj addTarget:self action:@selector(aggiungiTag:) forControlEvents:(UIControlEventValueChanged | UIControlEventTouchDragInside)]; //aggiungo lo switch alle celle cell.accessoryView = switchObj; NSInteger row = indexPath.row; [arrBoolSwitch insertObject:[NSNumber numberWithBool:NO] atIndex:row]; [switchObj release]; return cell; } 
+4
source share
3 answers

It all depends on your table, code, etc. etc., but if you are just looking for a string, one option would be to set the UISwitch tag UISwitch to indexPath.row (or some kind of value where you can calculate this). Sort of:

 [switchObj setTag:indexPath.row]; 

Then you will have the row id in your method:

 - (void)aggiungiTag:(id)sender { NSLog(@"the tag value is: %d, row is %d", [sender isOn], [sender tag]); return; } 

It all depends on the end goal and what the rest of the application does, but it's cheap and easy.

+2
source

Just configure the event handler to accept the event parameter:

 - (void)aggiungiTag:(id)sender forEvent:(UIEvent*)event { NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: [[[event touchesForView:sender] anyObject] locationInView:self.tableView]]; // do stuff with indexPath } 

Remember to also change the signature in @selector:

 [switchObj addTarget:self action:@selector(aggiungiTag::) // <-- two colons now :) forControlEvents:(UIControlEventValueChanged | UIControlEventTouchDragInside)]; 
+2
source

This is the solution !;)

 - (void)aggiungiTag:(id)sender { //recupero l'oggetto switch di cui ho bisgno UISwitch *theSwitch = (UISwitch *)sender; //recupero la cella della tabella sulla quale รจ posizionato lo switch UITableViewCell *cell = (UITableViewCell *)theSwitch.superview; UITableView *tableView = (UITableView *)cell.superview; NSIndexPath *indexPath = [tableView indexPathForCell:cell]; NSLog(@"the row is: %d", indexPath.row); NSLog(@"the tag value is: %d", [theSwitch isOn]) } 

Thanks everyone!

0
source

All Articles