views:

245

answers:

1

I have a custom UITableViewCell which I created in Interface Builder. I am successfully Dequeuing cells, but as I scroll, the cells appear to begin calling different indexPaths. In this example, I am feeding the current indexPath.section and indexPath.row into the customCellLabel. As I scroll the table up and down, some of the cells will change. The numbers can be all over the place, but the cells are not skipping around visually.

If I comment out the if(cell==nil), then the problem goes away.

If I use a standard cell, the problem goes away.

Ideas why this might be happening?

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"CalendarEventCell"];
    if (cell == nil) {
        NSLog(@"Creating New Cell !!!!!");
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CalendarEventCell" owner:self options:nil];
        cell = (UITableViewCell *)[nib objectAtIndex:0];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }


   // Set up the cell...
  [customCellLabel setText:[NSString stringWithFormat:@"%d - %d",indexPath.section, indexPath.row]];


    return cell;
}
A: 

When you create a cell in if(cell==nil) condition, are you assigning a reuseIdentifier to the cell as "CalendarEventCell"? I can see your nib name is CalendarEventCell, but I guess you would also need to set

cell.reuseIdentifier = @"CalendarEventCell";

If not, then I am not sure if it can deque the correct cells. Also, not sure what customCellLabel refers to.

Hetal Vora
When you use a nib to create a reusable cell, you set the reuseIdentifier in Interface Builder. I have set the Reuse Identifier to be the same as the filename. I believe it is correctly dequeuing cells because I get a few of the NSLogs when the table loads, then no additional NSLogs when I start scrolling up and down.
Chris
Also, you can't set cell.resuseIdentifier that way. reuseIdentifier is readonly.
Chris
Sorry, I missed that one. You are right. Its a readonly property and it can only be set when creating a cell to be reused.
Hetal Vora
Turns out, if you are reusing cells, you need to create everything in the cell programatically.
Chris