TonyTakeshi
TonyTakeshi

Reputation: 5929

UITable load first UITableViewCell for my last row of UITableViewCell

While coding with UITableView, I encounter this bug that I couldn't understand. I got 3 rows for my table and setting 50px for each row, except the 2nd row that I set it 500px which fill the whole screen. Problem comes when I scroll down to bottom, my bottom role is repeating first row appearance.This is my custom code:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return 3;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"TestTable";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        if (indexPath.section ==0 && indexPath.row==0) {
            cell.contentView.backgroundColor = [UIColor redColor];
        }       
        if (indexPath.section ==0 && indexPath.row==1) {
            cell.contentView.backgroundColor = [UIColor blueColor];
        }
        if (indexPath.section ==0 && indexPath.row==2) {
            cell.contentView.backgroundColor = [UIColor greenColor];
        }


    }
    // Configure the cell...
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {  
    if (indexPath.row==1) {
        return 400;
    }
    return 50; 
}  

According to the code, first row is red color, second role is red and third one is blue. But my last row is red color (repeating the first row). If I set my 2nd row to shorter than the screen, e.g. 100px, third row loading fine.

Original Display

enter image description here

After Scroll Down

enter image description here

Appreciate for all kind of advice, as I am running out of ideas how is this happening.

Thanks

Upvotes: 2

Views: 618

Answers (1)

hoha
hoha

Reputation: 4428

When 2nd row is long first row becomes invisible and its cell is reused in last row. That's why you should configure cell each time tableView:cellForRowAtIndexPath: is called i.e. after if (cell == nil) {...} block:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"TestTable";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...
    if (indexPath.section == 0 && indexPath.row==0) {
        cell.contentView.backgroundColor = [UIColor redColor];
    }       
    if (indexPath.section ==0 && indexPath.row==1) {
        cell.contentView.backgroundColor = [UIColor blueColor];
    }
    if (indexPath.section ==0 && indexPath.row==2) {
        cell.contentView.backgroundColor = [UIColor greenColor];
    }

    return cell;
}

Upvotes: 1

Related Questions