didSelectRowAtIndexPath有几个部分

时间:2012-05-04 21:30:44

标签: objective-c xcode uitableview ios5

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

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

1 个答案:

答案 0 :(得分:2)

您需要存储选择的indexPaths,然后在您的cellForRowAtIndexPath函数中,您还需要根据保存的indexPaths设置accessoryType。如果更改单元格上的accessoryType然后滚动,则该单元格被回收后,将继续检查accessoryType。