The first line in a UITableview is different from the other lines

My UITableView nedd should have an image in the first line. What is the problem? that when the user scrolls the table view, and after that he scrolls upwards, information about other rows appears in the image! Do you know, why?

This is my code (I use UITableViewCell)

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row != 0) {
    return 73.0;
}
else {
    return 109.0;
}   
}

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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    if (indexPath.row == 0) {
        [[NSBundle mainBundle] loadNibNamed:@"customcell_3" owner:self options:NULL];
        cell = cellaNib_with_image;
    }
    else {
        [[NSBundle mainBundle] loadNibNamed:@"customcell_2" owner:self options:NULL];
        cell = cellaNib;
    }


}

if (indexPath.row == 0) {
    UIImage *rowBackground;
    UIImage *selectionBackground;
    rowBackground = [UIImage imageNamed:@"image.png"];
    selectionBackground = [UIImage imageNamed:@"image.png"];
    cell.backgroundView = [[[UIImageView alloc] init] autorelease];
    ((UIImageView *)cell.backgroundView).image = rowBackground;
    cell.selectedBackgroundView = [[[UIImageView alloc] init] autorelease];
    ((UIImageView *)cell.selectedBackgroundView).image = selectionBackground;

}
else {
        NSString *elemento = [NSString stringWithFormat:@"%@", [array objectAtIndex:indexPath.row]];
    UILabel *testoLabel = (UILabel*)[cell viewWithTag:1];
    testoLabel.text = elemento;
//ecc... here a take the other datas
}
return cell;
}

Thank!

+3
source share
1 answer

When cells scroll, they are reused.

So, the first cell can be reused for other cells, and vice versa.

I would use two CellIdentifiers, one for the first row and the second for the rest of the rows.

If indexPath.row == 0, create / delete a cell using CellID1 and configure it and return.

indexPath.row > 1, / CellID2, .

single cellID, nil/ reset , .

+10

All Articles