views:

30

answers:

1

Hi All

Odd question, I am creating a UITableView, setting 8 rows of data. Simply, I'm just returning the indexpath row to try and figure out what is wrong, but ultimately, any rows that are off screen seem to load in a strange order.

For example, rows 0,1,2,3,4,5 all appear ok on first load, but then 6 & 7 are off screen. When I scroll to them I usually see rows 6 and 0, or 6 and 1. I then see row 7 appear back at the top of the table when I scroll back up.

Here's my code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        [cell.textLabel setText:[NSString stringWithFormat:@"Row: %d", [indexPath row]]];
    }

    return cell;
} 

Am I doing something really stupid? Cheers

A: 

Create your cell in the if statement and then set the properties (ie the label) outside the if statement.

Rudiger
Actually, creating the cell outside of the if statement is correct (as per Apple's documentation.). I moved the label properties outside and it works great though! Thanks :-)
mootymoots
Thats odd, because if you don't have a cell it should create the cell. Now if a cell exists it should reuse it but the text might be of the old value, so you should reset the label
Rudiger
If you create a blank navigation iphone app with xcode it gives you the above code (without the label part)
mootymoots
Only the label should be set outside the if statement. The template is correct for creating the cell.
Rudiger