mootymoots
mootymoots

Reputation: 4575

UITableViewCell showing indexpath rows out of order

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

Upvotes: 2

Views: 1032

Answers (2)

Prerna
Prerna

Reputation: 369

You can use

NSString *cellIdentifier =[NSString stringWithFormat:@"%d",indexPath.row];

instead of

static NSString *CellIdentifier = @"Cell";

Upvotes: 1

Rudiger
Rudiger

Reputation: 6769

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

Upvotes: 0

Related Questions