Prateek sharma
Prateek sharma

Reputation: 239

Scrolling of tableview removes checkmarrk from uitableview cell

I'm using tableview and I'm doing multiple check on the uitableview . Everything goes perfect , I', getting the correct values but when I scroll the table view , it lost the checkmark image(using default checkmark no custom image) but selected value retains in the array ...

Scrolling the table view removes the check mark image.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    AppDelegate *app= (AppDelegate *)[[UIApplication sharedApplication]delegate];

    if([_tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark){

    NSLog(@"yes");

    [placesvisitedarray removeObject:[app.nameArray objectAtIndex:indexPath.row]];


    [_tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;

    }
    else
    {
        NSLog(@"no");

        [_tableView cellForRowAtIndexPath:indexPath].accessoryType =  UITableViewCellAccessoryCheckmark;

        [placesvisitedarray addObject:[app.nameArray objectAtIndex:indexPath.row]];

    }
   // [_tableView reloadData];

}

Upvotes: 0

Views: 250

Answers (1)

Hesham
Hesham

Reputation: 5274

The checkmark is being removed because as you scroll the tableview cellForRowAtIndexPath: is being called and cells are recreated.

You could write a method to check if a certain value exists in your array:

- (BOOL)stringExistsInPlacesVisited:(NSString *)stringToMatch {
    for (NSString string in placesvisitedarray) {
        if ([string isEqualTo:stringToMatch])
            return YES;
    }
    return NO;
}

Then in cellForRowAtIndexPath: you have to check placesvisitedarray and insert/remove the checkmark.

if ([stringExistsInPlacesVisited:[app.nameArray objectAtIndex:indexPath.row])
    cell.accessoryType =  UITableViewCellAccessoryCheckmark;
else
   cell.accessoryType =  UITableViewCellAccessoryNone;

Code not tested so it might not work, but at least it will give you an idea on how to proceed.

Upvotes: 1

Related Questions