开发者

UITable load first UITableViewCell for my last row of UITableViewCell

开发者 https://www.devze.com 2023-02-28 03:18 出处:网络
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 sc

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.
-开发者_JAVA百科 (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

UITable load first UITableViewCell for my last row of UITableViewCell

After Scroll Down

UITable load first UITableViewCell for my last row of UITableViewCell

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

Thanks


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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消