My UITableView has duplicated rows

Posted by Mark on Stack Overflow See other posts from Stack Overflow or by Mark
Published on 2010-06-08T02:37:21Z Indexed on 2010/06/08 2:42 UTC
Read the original article Hit count: 259

Filed under:
|
|

Im not sure why, but my UITableView, which isnt anything fancy, is showing repeating rows when it shouldnt be.

It seems that the rows that get added when the user scrolls (i.e. the rows that are off the screen to start with) are getting the data for the wrong row index. Its almost like when a new cell is de-queued, it's using a cell that 'was' used, but wasn't cleaned up correctly.

Do you need to 'clean up' cells that are de-queue so that new cells dont use cells that are already created?

my code is as below:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CustomCellIdentifier = @"CustomCellIdentifier";

    MyDayCell *cell = (MyDayCell *)[tableView 
                                      dequeueReusableCellWithIdentifier: CustomCellIdentifier];

    if (cell == nil) { 
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyDayCell" owner:self options:nil];

        for (id oneObject in nib) 
            if ([oneObject isKindOfClass:[MyDayCell class]])
                cell = (MyDayCell *)oneObject;

    } 

    NSUInteger section = [indexPath section];
    NSUInteger row = [indexPath row]; 
    NSArray *thisSectionItems = (NSArray*)[self.listData objectForKey: [[NSNumber alloc] initWithInt:section]];
    MyDayDetails *rowData = [thisSectionItems objectAtIndex:row]; 

    //setup my cells data here...

    return cell;
}

Is there anything wrong with this code?

has anyone seen anything like this before?

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c