滚动tableview时,UISwitch会更改单元格

时间:2010-09-05 13:11:07

标签: objective-c uitableview uiswitch

我有一个奇怪的问题(至少在我看来)。当我向表视图添加UISwitch时,当用户滚动表视图时,开关会自动更改单元格。下面是关于如何为tableview创建UISwitch的代码。

- (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;
}

有人可以让我知道为什么开关不断移出指定的单元格,也可以采取补救措施。

Justin Gallagher建议:

这种情况正在发生,因为您正在缓存细胞并重复使用它们。有时带开关的电池会在另一个地方重复使用。尝试使用开关为单元格设置不同的标识符。

但有人可以告诉我如何为每个细胞设置不同的标识符吗?

干杯,

iSee的

2 个答案:

答案 0 :(得分:2)

这种情况正在发生,因为您正在缓存细胞并重复使用它们。有时带开关的电池会在另一个地方重复使用。尝试使用开关为单元格设置不同的标识符。

答案 1 :(得分:0)

我使用以下代码here [原始来源]来解决我的问题。我已粘贴下面的代码段。此外,Justin Gallagher给了我这个想法。谢谢。

- (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