views:

41

answers:

2

I have a selectedArray defined as a property in my file that holds which cells have been "checked". When displaying the cell I check whether or not the current cell value is in the selectedArray, and if it is, I display the UITableViewCellAccessoryCheckmark as the table's Accessory View.

The only problem is, when the user searches for through the UITableView using the UISearchBar, I ask the table to reloadData when they are done searching, and in the numberofRows method I see the right amount of objects in the selectedArray. However, the cellForRowatIndexPath method still displays the old checkmarks as if the selectedArray has never been updated.

Can anyone PLEASE help me? Here's my code -

NSArray *tempArray = [[NSArray alloc] init];
if(tableView == theTable) {
    tempArray = [contactsArray objectAtIndex:indexPath.row];
} else {
    tempArray = [searchfor_array objectAtIndex:indexPath.row];
}

NSString *cellValue = [tempArray objectAtIndex:0];
static NSString *CellIdentifier;
CellIdentifier = cellValue;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(70, 30, 200, 20)];
    name.text = [tempArray objectAtIndex:0];
    name.backgroundColor = [UIColor clearColor];
    [name setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]];
    [cell.contentView addSubview:name];
    [name release];

    if([selectedArray containsObject:[tempArray objectAtIndex:0]]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }

}

return cell;
[cell release];
[tempArray release];
[cellValue release];
+1  A: 

It looks as though you're setting up the cell once and reusing it. Step through the code and you should see that the if block is entered only the first time the cell is loaded.

You'll want to adjust the cell for each row, like so:

if (cell == nil) {
    // Initialize cell.
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

// Adjust cell for each row.
if([selectedArray containsObject:[tempArray objectAtIndex:0]]) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
Justin
When I do this though, it doesn't solve the problem AND the labels start stacking up on one another...
Raphael Caixeta
I didn't detail how to setup the cell and missed an else to clear the accessory view. Raphael's got it covered.
Justin
A: 

Adding to Justin's answer -

You should be adding the label in the if(cell == nil) part only. You handle the accessory view OUTSIDE of that statement. Here's what it should look like -

if (cell == nil) {

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(70, 30, 200, 20)];
    name.text = [tempArray objectAtIndex:0];
    name.backgroundColor = [UIColor clearColor];
    [name setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]];
    [cell.contentView addSubview:name];
    [name release];


}

if([selectedArray containsObject:[tempArray objectAtIndex:0]]) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
    cell.accessoryType = UITableViewCellAccessoryNone;
}
Raphael Caixeta
Thanks for filling the gaps in my answer.
Justin