views:

62

answers:

2

Hi, I have a weird problem (at least in my opinion). When I add a UISwitch to my table view, the switch changes cell automatically when the user scrolls the table view. Below is the code on how I create the UISwitch for the tableview.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        //NSLog(@"This is what we are looking for %@    %@", checkUsername, checkPassword);
        // Configure the cell...
    NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
    NSArray *array =[dictionary objectForKey:@"Settings"];
    NSString * cellValue = [array objectAtIndex:indexPath.row];

    static NSString *CellIdentifier = @"Cell";
    NSUInteger section = [indexPath section];
    NSUInteger row = [indexPath row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell.accessoryView = nil;


    if (!cell) {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
        cell.textLabel.adjustsFontSizeToFitWidth = YES;
            //switcher.on = [[NSUserDefaults standardUserDefaults]boolForKey:@"Settings"];

        if (section == 1)
        {

            UISwitch *switchView = [[UISwitch alloc] init];
            cell.accessoryView = switchView;
            [switchView setOn:YES animated:NO];
            [switchView setTag:[indexPath row]];
            [switchView addTarget:self action:@selector(switchWasChangedFromEnabled:) forControlEvents:UIControlEventValueChanged];
                //[self.tableView reloadData];
            [switchView release];
        }

    }
    return cell;
}

Can someone please let me know why the switches keep moving out of the designated cells and also a possible remedy.

Justin Gallagher suggested this:

It's happening because you're cacheing your cells and reusing them. Sometimes the cell with the switch is getting reused in another place. Try setting a different identifier for the cell with the switch.

But can someone tell me how I can set different identifier for each cell?

Cheers,

iSee

+1  A: 

It's happening because you're cacheing your cells and reusing them. Sometimes the cell with the switch is getting reused in another place. Try setting a different identifier for the cell with the switch.

Justin Gallagher
A: 

Hi,

I used the following code presented here [original source] to solve my issue. I have pasted the code snippet below. Also, this idea was given to me by Justin Gallagher. Thank You.

- (void)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (<indexPath is for cell type "A">) {
    static NSString *SomeIdentifierA = @"SomeIdentifierA";

        // This could be some custom table cell class if appropriate    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SomeIdentifierA];
    if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SomeIdentifierA] autorelease];
            // Any other attributes of the cell that will never change for type "A" cells.
    }

        if (someCondition) {
            cell.textColor = <someColor>;
        } else {
            cell.textColor = <anotherColor>;
        }
        cell.text = <some text>;    
    return cell;
    } else if (<indexPath is for cell type "B") {
    static NSString *SomeIdentifierB = @"SomeIdentifierB";

        // This could be some custom table cell class if appropriate    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SomeIdentifierB];
    if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SomeIdentifierB] autorelease];
            // Any other attributes of the cell that will never change for type "B" cells.
    }

        cell.text = <some text>;    
    return cell;
    } else {
        return nil; // Oops - should never get here
iSee