objective-ciosuitableviewcheckmarkaccessorytype

Cell accessorytype doesn't reload on [tableview reloadData]


I'm very new to iPhone programming, so my problem might be caused by total lack of knowledge of very basic principles.

I'm developing an iPhone app that has two Views. The first view has two buttons, when one of the buttons is pressed, a modalview popsup with a tableview. This table view is populated differently depending of what button is pressed. If button button1 is pressed the tableview is populated with the button1Data. The user can select cells, and if this is done the cells accessorytype is set to UITableViewCellAccessoryCheckmark. I then save the name of the checked cells into tableViewListChecked, so it can later be decided what cell are supposed to be checked as the data changes.

The problem is as following: after the modalview is dismissed, and I select button2 the cells that was selected in button1Data is still selected in button2Data. It is clear to me that the function [theTableView reloadData] is working since the data does change, however the accesorytupes is still the same until i scroll the cells off the screens.

P.S. If I do in fact scroll cells of the screen there accessorytype is set correctly.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    else if ([tableViewListChecked containsObject:[NSString stringWithString:cell.textLabel.text]]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    }

    else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    cell.textLabel.text = [tableViewList objectAtIndex:indexPath.row];

    return cell;
}

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

    if (![tableViewListChecked containsObject:[NSString stringWithString:cell.textLabel.text]]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [tableViewListChecked addObject:[NSString stringWithString:cell.textLabel.text]];       
    }

    else if ([tableViewListChecked containsObject:[NSString stringWithString:cell.textLabel.text]]) {
        cell.accessoryType = UITableViewCellAccessoryNone;
        [tableViewListChecked removeObject:[NSString stringWithString:cell.textLabel.text]];
    }   
}

Thanks for you help!


Solution

  • If the cell is not nil (if it has been dequeued from the table view) then you don't actually, as it is, change the accessory type. Drop the else, ie change

    else if ([tableViewListChecked containsObject:[NSString stringWithString:cell.textLabel.text]]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    }
    

    to

    if ([tableViewListChecked containsObject:[NSString stringWithString:cell.textLabel.text]]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    }