0

I have a UITableView with 3 custom UITableViewCells that I'm currently dequeuing like so:

    if (indexPath.row == 0) {
         static NSString *CellIdentifier = @"MyCell1";
         MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
         if (cell == nil) {
             cell = [[[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
         }
         return cell;
    }
    if (indexPath.row == 1) {
         static NSString *CellIdentifier = @"MyCell2";
         MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
         if (cell == nil) {
             cell = [[[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
         }
         return cell;
    }
    if (indexPath.row == 2) {
         static NSString *CellIdentifier = @"MyCell3";
         MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
         if (cell == nil) {
             cell = [[[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
         }
         return cell;
    }

I've tried to do this multiple ways, but the problem is even though I'm still dequeueing them all with different identifiers, when I scroll the tableView around, sometimes my first cell appears in the location of my third cell and viceversa. There seems to be some weird caching going on.

Does anyone know why? Thanks.

Charles
  • 50,943
  • 13
  • 104
  • 142
VTS12
  • 452
  • 8
  • 22
  • What if you remove 'static' from the cell identifier declarations? – Phillip Mills Jun 09 '12 at 01:35
  • It doesn't make a difference. In the Apple documentation, even they use static NSString *CellIdentifier. – VTS12 Jun 09 '12 at 01:38
  • You should really have three different identifier variables though, since you have three different values. Statics are only initialised once. Nowhere I this code do you appear to actually put any information _in_ the cells, where do you do that? Can you include a screenshot of the problem? And are there really only ever three rows? – jrturton Jun 09 '12 at 05:55

1 Answers1

1

Since you are always allocating the same cell classes, there is no point to the code you posted. The cell identifiers are not used to identify the specific cell, but the subclass you are using.

So change the code to:

static NSString *CellIdentifier = @"MyCell";
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
     cell = [[[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
return cell;

and set up the cell contents properly in willDisplayCell based on indexPath.section and indexPath.row:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
EricS
  • 9,650
  • 2
  • 38
  • 34
  • so in willDisplayCell:forRowAtIndexPath: I should change/set things like custom cell labels, edit custom cell textfields, etc.? I thought according to the Apple docs, you should do this by retrieving the view for the subview tags? – VTS12 Jun 09 '12 at 05:06
  • If you are going to create unique subviews, do it in cellForRowAtIndexPath and then set up the values in willDisplayCell. In the code you posted, all of your UITableViewCells were essentially identical, with no unique subviews, so there was no need for 3 identifiers. What I normally do is create a "fillCellWithData" method and call it from both cellForIndexPath and willDisplayCell. – EricS Jun 09 '12 at 16:59
  • Think of cellForIndexPath as what creates the views or finds an unused one that already exists and willDisplayCell as another time you have to fill it with data. – EricS Jun 09 '12 at 17:06