Chaudhry Talha
Chaudhry Talha

Reputation: 7868

UISwitch in CustomCell is not changing the value at specific indexpath

This is the type of screen I'm making: screen 1
So when the UISwitch state changes it should change the label to ON or OFF. In my cellForRowAtIndexPath I'm calling [cell.mainSwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged]; and switchChanged is as follow:

-(void) switchChanged:(id)sender {
    UISwitch* cellNew = (UISwitch*)sender;

        if ([cellNew isOn]) {
            cell.funcStatus.text = [funcStatusArr objectAtIndex:0];
        } else {
            cell.funcStatus.text = [funcStatusArr objectAtIndex:1];
        }
}

Now the problem I'm facing is that it is not changing the Label at the specific cell butt all the switches are changing the Label of 4th(last) cell as shown in the figure below. problem
As you can see that the first label is off but it is changing the label of last row. Any ideas why it is happening or how to tell the functions that this index.row is sending the request.

Upvotes: 0

Views: 379

Answers (2)

Konstantin  Kryzhanovsky
Konstantin Kryzhanovsky

Reputation: 1001

So u must add tag property to all UISwitch. Fasters way its in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath call

 // code
 // its good work if u have only 1 section
 mySwitch.tag = indexPath.row.
 //code 

Than fix u're code

-(void) switchChanged:(UISwitch*)switch {
    SettingsTableViewCell *selectedCell;
    NSIndexPath *selectedIndexPath;
    if(switch.tag == 0)
      // create selectedIndexPath with correctrly row and section
       selectedIndexPath = [NSIndexPath indexPathForRow:'YourRow'inSection:'Your section']
      // create cell
       selectedCell = [self.tableView cellForIndexPath:selectedIndexPath];
     } else if(switch.tag == 1) {
      // same logic;
     }

    // and replace text for selected cell
    if ([switch isOn]) {
        selectedCell.funcStatus.text = [funcStatusArr objectAtIndex:0];
    } else {
        selectedCell.funcStatus.text = [funcStatusArr objectAtIndex:1];
    }
}

it's must work.

Upvotes: 1

Burhan Ahmad
Burhan Ahmad

Reputation: 728

You doing wrong thing.First you need to get cell at specific changed event. follow this code.

-(void) switchChanged:(id)sender {



    UISwitch* switchBtn = (UISwitch*)sender;
    //self.tableView is UITableView's outlet named tableView
    CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
    //First detect Tableview's Cell then do the stuff
    //CustomCellTableViewCell replace it with you custom cell class
    CustomCellTableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

    if ([switchBtn isOn]) {
        cell.funcStatus.text = [funcStatusArr objectAtIndex:0];
    } else {
        cell.funcStatus.text = [funcStatusArr objectAtIndex:1];
    }
    [self.tableView reloadRowsAtIndexPaths:[[NSArray alloc] initWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];


}

Cheers.

Upvotes: 1

Related Questions