views:

29

answers:

3

Hi All

I basically want to show a UITableViewCellAccessoryDisclosureIndicator depending on certain situations, in this case, where a URL exists in my row data (which I reference via indexPath.row.

I have the right code to figure that out (ie if an URL exists), but I am unsure where I should state this code in the "cellForRowAtIndexPath:" method. I've tried it inside the if(cell == nil) clause and outside of it, and both times the cell is redrawn sometimes with or without the indicator when the cell comes back into view from scrolling.

It works fine on initial draw.

Any tips on how I can best achieve this?

A: 

Soon after you check for your condition, you can set the cell you're interested in by its indexPath. Then, mayb,e you want to call reloadData.

rano
This is effectively what I am doing - I see no need to reloadData as there is no new data to reload. I simply want the row to show or not show an indicator based on existing data... It shows the right label, so why not the right indicator?
mootymoots
it was an expedient to force redrawn
rano
A: 

The answer was simple after all. When a URL was present I was setting a UITableViewCellAccessoryDisclosureIndicator. What I wasn't doing was setting a UITableViewCellAccessoryNone if there wasn't a URL present. Therefore upon redraw it was remembering a row with it set to on.

mootymoots
+1  A: 

Yes, you should put it in the cellForRowAtIndexPath: delegate method, outside of the "if (cell == nil)" statement. Since the cells are recycled, you have to make sure that the accessory type is set to the right value for every cells and not only the one(s) that need specific accessory type.

For example :

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)ip {
    UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:@"CELL"];

    if (cell == nil) {
        cell = [[[UITableView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"] autorelease];
    }

    if (ip.row == 3) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    return cell;
}

This code would not work the way we want it to. Since the cells are recycled, the cell with accessory type set to UITableViewCellAccessoryDisclosureIndicator will be reused later at an undefined row and would then exhibit a disclosure indicator even if it is not located at row #3.

To deal with cells reusing, we have to set accessory type for EVERY cells, whatever their row :

    if (ip.row == 3) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    else {
        cell.accessoryType = UiTableViewCellAccessoryNone;
    }

I don't know for sure if this explains your problem, but it sure looks related to cell reusing.

EDIT :

OK, you found the solution by yourself while I was typing my answer. :o)

Eric MORAND