Question

I have a UITableView that has a UISegmentedControl in each cell. When the value of the UISegmentedControl is changed, I want to change a value in a MYSQL database. (It's a public/private switch)

I have this in my cellForRowAtIndexPath

[cell.pubPrivSegmentedControl addTarget:self action:@selector(switchValue:) forControlEvents:UIControlEventValueChanged];

and then this is my method

-(void)switchValue:(id)sender
{
    UISegmentedControl *sc = (UISegmentedControl *)sender;
    NSString *status = [sc selectedSegmentIndex] ? @"Private" : @"Public";
    NSLog(@"status is %@", status);
    //call method to change value in database using indexPath.row of tapped UISC
}

My question is how to pass the indexPath.row value to the selector when the segmented controller is tapped. Thanks.

Was it helpful?

Solution

You can add target in this way:

[cell.pubPrivSegmentedControl addTarget:self action:@selector(switchValue:event:) forControlEvents:UIControlEventValueChanged];

And implement method :

- (void)switchValue:(id)sender event:(id)event {


    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentTouchPosition = [touch locationInView: tableView];
    NSIndexPath *indexPath = [tableView indexPathForRowAtPoint: currentTouchPosition];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top