2011-08-08 13 views
1

私のiPhoneアプリケーションには、24時間すべてのリストを含むグループ化されたテーブルビューがあります。ユーザーがテーブルビューでセルの1つを選択すると、アクセサリビューにチェックマークが表示され、NSUserDefaultsを介してデータが保存されます。ユーザーが前方にビューをロードすると、これはNSUserDefaultsから情報を引き出し、対応するセルにチェックマークを表示する必要があることを意味します。このビューでスクロールを開始するまで、これは正常に動作します。そして、この問題が発生した:UITableViewは上下にスクロールしてセルを選択しています

enter image description here

私が設定している私のdidSelectRowAtIndexPathまで1行が選択された場合に、他のすべての選択が解除されるように。これはうまく動作しますが、リストをスクロールするだけで、一見無作為な項目が選択され始めます。数回スクロールしていくと、すべてのアイテムが選択されます。セルの1つをクリックすると、その1つのセルが選択され、他のセルは選択解除されます(これが選択されている必要があります)。

しかし、どのようにセルがスクロールするだけで選択されますか?ここでの問題はおそらく配置されている私の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

関連する問題