一次多次检查accesoryType

时间:2012-04-16 14:00:53

标签: ios xcode

所以,有一个问题:当我检查表格中的单元格时,另一个单元格也被检查,但我只想要一次检查。有消息来源:

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

2 个答案:

答案 0 :(得分:0)

尽量不重复使用该单元格。 因为当您重复使用单元格时,您也会重复使用checkmark- / accessory-state。 那么为什么不删除单元格排队。

您可以尝试:

.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: 现在它还应该存储复选标记状态!

答案 1 :(得分: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;
}

优点是:

  • 你仍然可以重复使用细胞;
  • 即使视图由于内存不足警告而被卸载,它也会在重新加载表视图后恢复复选标记。