2012-05-04 17 views
0

我有4个部分在我的tableview中,当一行被按下时,我想要一个复选标记添加到该行。使用下面的代码,如果我选择一行,其他一些行也会被检查,这是不希望的。我猜这与几个部分有关,但我不知道如何检查或修复它。我的_allTagsList是一个包含4个字典的NSMutableArray。didSelectRowAtIndexPath与几个部分

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSDictionary *dictionary = [_allTagsList objectAtIndex:indexPath.section]; 
    NSArray *array = [dictionary objectForKey:@"Tags"]; 
    NSString *selectedTag = [array objectAtIndex:indexPath.row]; 


    if([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark) 
    { 
     [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone; 
    } 
    else 
    { 
     [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; 
    } 

    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 

回答

2

您需要存储选择哪些indexPaths,然后在你的cellForRowAtIndexPath功能,你需要t结合设置基于这些保存indexPaths的accessoryType。如果您更改单元格中的accessoryType,然后滚动,那么该单元将被回收,accessoryType将保持检查状态。

+0

我不确定那会怎么做? –

+0

@NielsSønderbæk为可变数组创建一个属性,并在视图加载时实例化它。每当有人选择一个单元格检查索引路径是否已经在数组中,并且是否将其删除并将单元格设置为未选中状态。否则,添加它并设置单元格检查。然后在索引路径的行中的单元格中检查索引路径是否在数组中,并设置单元格是否被设置为未检查。 –

+0

谢谢,它现在的作品:) –