0

したがって、問題があります。テーブル内のセルをチェックすると、別のセルもチェックされますが、一度に1つのチェックだけが必要です。ソースがあります:

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

    cell.textLabel.text = [[self.symptoms objectAtIndex:indexPath.row ] objectForKey:@"name"];


    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


    NSLog(indexPath);
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
        cell.accessoryType = UITableViewCellAccessoryNone;

    } else {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
4

2 に答える 2

0

次のプロパティをテーブルビューコントローラに追加します。

@property(nonatomic, retain) NSIndexPath* selectedIndexPath;

テーブルビューコントローラの実装でプロパティを合成します。

@synthesize selectedIndexPath;

この行をに追加しdeallocます:

self.selectedIndexPath = nil;

これをに追加しtableView:cellForRowAtIndexPath:ます:

cell.accessoryType == (indexPath.section == self.selectedIndexPath.section && indexPath.row == self.selectedIndexPath.row) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;

これをに追加しtableView:didSelectRowAtIndexPath:ます:

if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
    cell.accessoryType = UITableViewCellAccessoryNone;
    self.selectedIndexPath = nil;
} else {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    self.selectedIndexPath = indexPath;
}

利点は次のとおりです。

  • セルは引き続き再利用できます。
  • メモリ不足の警告の結果としてビューがアンロードされた場合でも、テーブルビューがリロードされた後にチェックマークが復元されます。
于 2012-04-16T14:56:29.103 に答える
0

セルを再利用しないようにしてください。セルを再利用すると、チェックマーク/アクセサリの状態も再利用されるためです。では、セルのキューイングを削除してみませんか。

あなたが試すことができます:

.h ファイル:

@property (nonatomic, retain) NSMutableDictrionary *checkedState;

.m ファイル: (上部)

@synthesize checkedState = checkedState_;

(休み)

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

    UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                       reuseIdentifier:nil] autorelease];

    cell.textLabel.text = [[self.symptoms objectAtIndex:indexPath.row ] objectForKey:@"name"];

    if(self.checkedState) {
      NSNumber *checkmarkStateNumber = [self.checkedState objectForKey:[NSNumber numberWithInt:indexPath.row]];
      if(checkmarkStateNumber && [checkmarkStateNumber boolValue] == YES) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
      }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


    NSLog(indexPath);
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    if(!checkedState) { self.checkedState = [NSMutableDictionary dictionary]; }


    BOOL checkmarkState = NO;
    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
        cell.accessoryType = UITableViewCellAccessoryNone;

    } else {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        checkmarkState = YES;
    }

    [self.checkedState setObject:[NSNumber numberWithBool:checkmarkState] forKey:[NSNumber numberWithInt:indexPath.row]];

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

更新 1: チェックマークの状態も保存する必要があります。

于 2012-04-16T14:42:16.420 に答える