2011-03-22 30 views
0

用我的表視圖的數據源方法的代碼波紋管(...的cellForRowAtIndexPath:)我創造UISwitch在只有某些細胞爲用戶設置查看進入一個酥料餅的視圖。UISwitch被添加到錯誤和隨機單元格中?

的是,當我向上和向下滾動視圖開關的正在增加,並從我沒有具體說明應該在他們UISwitch隨機池中取出由於某種原因,但是工作的開關。

我在這裏做錯了什麼?

這是我的一些代碼:

if (section == 0) { 

    if (row == 0) { 
     cell.textLabel.text = @"Units"; 
    } 
    if (row == 1) { 
     cell.textLabel.text = @"Prefered System"; 
    } 
    if (row == 2) { 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     cell.textLabel.text = @"Lock System"; 

     UISwitch *switch1 = [[UISwitch alloc] initWithFrame:switchFrame]; 
     if ([defaults boolForKey:kLockSystem]) { 
      [switch1 setOn:YES animated:NO]; 
     } 
     else { 
      [switch1 setOn:NO animated:NO]; 
     } 

     [switch1 addTarget:self action:@selector(switchToggled:) forControlEvents:UIControlEventValueChanged]; 
     switch1.tag = 1; 
     cell.accessoryView = switch1; 
     [switch1 release]; 
    } 
    if (row == 3) { 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     cell.textLabel.text = @"Reset On Exit"; 

     UISwitch *switch2 = [[UISwitch alloc] initWithFrame:switchFrame]; 
     if ([defaults boolForKey:kResetOnExit]) { 
      [switch2 setOn:YES animated:NO]; 
     } 
     else { 
      [switch2 setOn:NO animated:NO]; 
     } 

     [switch2 addTarget:self action:@selector(switchToggled:) forControlEvents:UIControlEventValueChanged]; 
     switch2.tag = 2; 
     cell.accessoryView = switch2; 
     [switch2 release]; 
    } 
} 

回答

2

我猜你正在使用dequeueReusableCellWithIdentifier:執行該代碼之前得到的細胞。您的問題是UISwitches在被該方法返回之前未從單元中移除。

與正在使用的代碼,應該有一個簡單的解決辦法:只需設置cell.accessoryView爲零對於沒有開關的行。或者在輸入發佈的代碼位之前,無條件地將它設置爲零。

+0

真棒感謝! – cgossain 2011-03-22 02:29:40

相關問題