0

我有很多tableview行,大约 60 行,我希望用户能够check或他想要的行数。问题是当我检查某个时,该位置的所有其他单元格也会被检查。例如,如果我检查第 5 行,第 15、25、35 等行也会得到 a ,我不希望这种情况发生。我需要检查的行只是用户真正选择的行。这是我的方法代码uncheckUITableViewCellAccessoryCheckmarkcellUITableViewCellAccessoryCheckmarkdidSelectRowAtIndexPath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{ 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    Materia *current = [Manejador MateriasEnListaAtIndex:indexPath.row ];
    if(cell.accessoryType == UITableViewCellAccessoryCheckmark){
        cell.accessoryType = UITableViewCellAccessoryNone;
        [ManejadorVistas EliminarMateria:current];
    }
    else{
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [ManejadorVistas AgregarMateria:current];
    }
}
4

2 回答 2

1

设置dequeueReusableCellWithIdentifiernil喜欢波纹管,然后尝试使用它...

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];

也在这种情况下编写你的代码..

if (cell == nil) {

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

     // write your code here....
}
于 2013-04-02T04:47:22.327 回答
1

在你的 cellForRowAtIndexPath

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

if (cell == nil) {

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];

     // write your code here....
}
Materia *current = [Manejador MateriasEnListaAtIndex:indexPath.row ];

//While reusing we have to check whether ManejadorVistas contains the object. 
//Because you would have added it inside didSelect Delegate

if([ManejadorVistas containsObject:current]) 
{
   cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
   cell.accessoryType = UITableViewCellAccessoryNone;
}
于 2013-04-02T04:52:42.903 回答