2011-08-08 54 views
1

我在我的iPhone應用程序中有一個分組表格視圖,其中包含一天中所有24小時的列表。當用戶選擇表視圖中的一個單元格時,它在其附件視圖中顯示覆選標記並通過NSUserDefaults保存數據。當用戶加載視圖時,這意味着我必須從NSUserDefaults中取出信息並在相應的單元格上顯示覆選標記。這工作正常,直到我開始在視圖中滾動。那麼出現這種情況:UITableView通過向上和向下滾動來選擇單元格

enter image description here

我已經把我的didSelectRowAtIndexPath起來,這樣當選擇了一個排,所有的人都取消。這工作得很好,但通過滾動列表,看似隨機的項目開始被選中。如果我繼續上下滾動幾次,所有項目都將被選中。如果我點擊其中一個單元格,則選中一個單元格,而其他單元格被取消選中(這是應該的方式)。

但是如何通過滾動選擇單元格?這裏是我的cellForRowAtIndexPath方法,該問題可能位於:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    NSString *cellValue; 

    if (indexPath.row == 0) { 
     cellValue = @"00:00"; 
    } else if (indexPath.row == 1) { 
     cellValue = @"01:00"; 
    } else if (indexPath.row == 2) { 
     cellValue = @"02:00"; 
    } else if (indexPath.row == 3) { 
     cellValue = @"03:00"; 
    } else if (indexPath.row == 4) { 
     cellValue = @"04:00"; 
    } else if (indexPath.row == 5) { 
     cellValue = @"05:00"; 
    } else if (indexPath.row == 6) { 
     cellValue = @"06:00"; 
    } else if (indexPath.row == 7) { 
     cellValue = @"07:00"; 
    } else if (indexPath.row == 8) { 
     cellValue = @"08:00"; 
    } else if (indexPath.row == 9) { 
     cellValue = @"09:00"; 
    } else if (indexPath.row == 10) { 
     cellValue = @"10:00"; 
    } else if (indexPath.row == 11) { 
     cellValue = @"11:00"; 
    } else if (indexPath.row == 12) { 
     cellValue = @"12:00"; 
    } else if (indexPath.row == 13) { 
     cellValue = @"13:00"; 
    } else if (indexPath.row == 14) { 
     cellValue = @"14:00"; 
    } else if (indexPath.row == 15) { 
     cellValue = @"15:00"; 
    } else if (indexPath.row == 16) { 
     cellValue = @"16:00"; 
    } else if (indexPath.row == 17) { 
     cellValue = @"17:00"; 
    } else if (indexPath.row == 18) { 
     cellValue = @"18:00"; 
    } else if (indexPath.row == 19) { 
     cellValue = @"19:00"; 
    } else if (indexPath.row == 20) { 
     cellValue = @"20:00"; 
    } else if (indexPath.row == 21) { 
     cellValue = @"21:00"; 
    } else if (indexPath.row == 22) { 
     cellValue = @"22:00"; 
    } else if (indexPath.row == 23) { 
     cellValue = @"23:00"; 
    } 

    if ([[prefs valueForKey:@"quiet_hours_time_interval_from"] isEqualToString:cellValue]) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     cell.textLabel.textColor = [UIColor colorWithRed:72.0/255 green:104.0/255 blue:152.0/255 alpha:1]; 
    } 

    cell.textLabel.text = cellValue; 

    return cell; 
} 

prefs等於[NSUserDefaults standardUserDefaults]和我保存(正確)選定單元的quiet_hours_time_interval_from關鍵的精確值。

希望你們可以幫忙。

回答

0

您在檢查prefs valueForKey後需要一個ELSE。您需要清除該ELSE中的accessoryType。

你所看到的是UITableView的默認行爲。單元格被緩存,並且只在需要時重新創建。這就是dequeueReusableCell頂端的一點。

因此,由於您沒有任何清除accessoryType的代碼,最終所有代碼都會被檢查。

+0

所以它的工作原理。解決方案簡單謝啦。 – wstr

相關問題