0

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

- (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

尽量不要重复使用该单元格。因为当您重用单元格时,您也重用了复选标记-/附件状态。那么为什么不删除单元排队。

你可以试试:

.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 回答
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 回答