Advertise here

Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

UITableViewCell :: How to NOT reuse a cell?

blueFLoyd8blueFLoyd8 Posts: 96Registered Users
edited November 2009 in iOS SDK Development
I'm aware that the iPhone caches cells in a tableView and this is a problem for me. The first cell of my table displays 'refreshing' and a UIActivityIndicator. Then, when the table is refreshed (asynchronous query to server returns info for the table) I want the first cell to say 'Refresh' and to remove the activity indicator. Unfortunately it seems like the phone is reusing the same cell from before and the activity indicator is still there.

What is the best way to solve this problem? I don't want to keep track of a pointer to the UIActivityIndicator because that seems like overkill.

[PHP]// Set up cell
static NSString *listIdentifier = @refreshCell;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:listIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:listIdentifier] autorelease];
} else {

if (listIsRefreshing) {
cell.textLabel.text = @Refreshing;
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(100, 10, 20, 20)];
[activityIndicator startAnimating];
[activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
[cell addSubview:activityIndicator];
[activityIndicator release];
} else {
cell.textLabel.text = @Refresh;
Post edited by blueFLoyd8 on


  • smithdale87smithdale87 Posts: 4,447iPhone Dev SDK Supporter @ @ @ @ @
    edited November 2009
    The way to not reuse cells is to remove the
    cell = [tableView dequeueReusableCell...]

    This will force the table view to always create a new cell. However, it is probably a very bad idea especially if you have a large table.

    EDIT: An idea..

    The way you're currently doing it is fine, you're adding an activity indicator view if it's refreshing. So the only thing you need to add is:

    In the "else" part, i.e. if not refreshing, check to see if the activityindicator exists on that cell already ( because it was reused ). If it does, then just remove it.

    This should solve your problem.
  • blueFLoyd8blueFLoyd8 Posts: 96Registered Users
    edited November 2009
    The seems logical. How would I check to see if it exists though?
  • smithdale87smithdale87 Posts: 4,447iPhone Dev SDK Supporter @ @ @ @ @
    edited November 2009
    Using the tag property.
    if( listIsRefreshing )
    /// setup and add acivityIndicator
       [b]activityIndicator.tag = 123;[/b]
     // list is not refreshing
    // ...
      UIActivityIndicatorView *activityIndicator = (UIActivityIndicatorView*)[cell viewWithTag: 123];
      if( activityIndicator != nil )
      { /// the view exists, but we dont want it there, so remove it!


    In the "if" statement, you need to also check if viewWithTag to see if it already has the activity view before adding a new one. You dont need to add multiple ones on top of each other.
  • RLScottRLScott Posts: 1,667Tutorial Authors, Registered Users @ @ @ @
    edited November 2009
    A more straight-forward way to segregate cells is to establish two different reuse IDs - one that is used only for the first row and a different ID that is used for everything else, instead of using listIdentifier for all cells. That way the system will maintain two separate pools of reusable cells. You will be able to benefit from the performance gain of reusing cached cells without having to worry about a "first row" cell getting reused for some other row, or vice versa. You should not have to abandon cell reuse just to manage that activity indicator.

    Robert Scott
    Ypsilanti, Michigan
Sign In or Register to comment.