1

I have a tableview with custom UITableViewCell. Cell have a UISwitch controls. I have added cell into the table view controller with the same action for all switches. I have added tag value of UISwitch in cellForRowAtIndexPath method.

I want to determine what switch was changed, when user changed the switch status on/off. Here I am setting action on UISwitch button.

- (void)viewDidLoad
{
    [super viewDidLoad];
    cell.switchbtn.userInteractionEnabled=YES;
    [cell.switchbtn setOn:YES];
    [cell.switchbtn addTarget:self action:@selector(switchToggled:) forControlEvents:   UIControlEventValueChanged];
    [cell.contentView addSubview:cell.switchbtn];
}

Here I am setting tag value of uiswitch

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellidentifier=@"cellid";
    cell=[tableView dequeueReusableCellWithIdentifier:cellidentifier];

    if (!cell)
    {
        [[NSBundle mainBundle] loadNibNamed:@"cellid" owner:self options:nil];

    }

    cell.switchbtn.tag=indexPath.row;
    NSLog(@"btn tag=%d",cell.switchbtn.tag);
    return cell;
}

Here I am calling switchToggled: method to get the uiswitch status.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
  [self switchToggled:cell.switchbtn];
}

I am gettting tag value and each time status is On .even i status is off.

- (void) switchToggled:(UISwitch *)sender {
    UISwitch *mySwitch = (UISwitch *)sender;

    NSLog(@"tag ==%@",mySwitch);
    if ([mySwitch isOn]) {
        NSLog(@"its on!");
    } else {
        NSLog(@"its off!");
    }
}
modusCell
  • 13,151
  • 9
  • 53
  • 80
square
  • 162
  • 2
  • 13
  • You should not save state in the View. I wrote some sample code for switches in a tableView a while ago, you might consider a dedicated datasource like I did in [this answer](http://stackoverflow.com/questions/9264207/ios-loop-through-cells-and-retrieve-data/9264395#9264395) – Matthias Bauch Aug 05 '14 at 15:37
  • @matthis Bauch i have used your solution.but its not working in my case can you give me some more help – square Aug 06 '14 at 06:37

2 Answers2

1

Use CGpoint with direct Cell's property to identify which row is selected and that cell's Uiswitch's state you can identify easily even more accurate and this is the one method am using and it is working fine for me.

- (void)switchToggled:(id)sender
{

    CGPoint switchPositionPoint = [sender convertPoint:CGPointZero toView:[self tableName]];
    NSIndexPath *indexPath = [[self tableName] indexPathForRowAtPoint:switchPositionPoint];

    BusinessHoursCell *cell = (BusinessHoursCell*)[self.tableName cellForRowAtIndexPath:indexPath];

    int tag=indexPath.row;

    if (tag==0)
    {
        if (cell.workingDaySwitch.on)
        {
            NSLog(@"its on!");
        }

        else
        {
            NSLog(@"its off!");
        }
    }
}
Yohan
  • 1,108
  • 9
  • 21
0

I'd use the following scheme: In cellForRowAtIndexPath:.. set current indexPath to cell. Create protocol with method like - (void)cellAtIndexPath:(NSIndexPath *)path changedSwitchStateToState:(BOOL)state make controller delegate for cells. Cell handles switch state change by calling that delegate method.

In controller you should use something for dataSource(probably custom class) which stores objects which are used to configure the cell and has a method similar to - (void)changeSwitchStateToState:(BOOL)state atIndexPath:(NSIndexPath *)path where it gets corresponding object and updates state.

Timur Kuchkarov
  • 1,155
  • 7
  • 21