简体   繁体   中英

UITableViewCellAccessoryCheckmark issue, many cells get checked by only checking one row

I have a tableview with many rows, approx 60 rows, and I want the user to be able to check or uncheck with a UITableViewCellAccessoryCheckmark as many rows as he wants. The problem is that when I check a certain cell , all the other cells in that position will be checked too. For example if I check the row number 5, the row number 15,25,35 and so on will get a UITableViewCellAccessoryCheckmark too, and I don't want that to happen. I need that the rows checked be only the ones that the user really selected. Here is my code for the didSelectRowAtIndexPath method

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

set dequeueReusableCellWithIdentifier to nil like bellow and then try to use it...

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];

also write your code in this bellow condition..

if (cell == nil) {

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

     // write your code here....
}

In your 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM