Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

First row in a UITableview, different from other rows

My UITableView nedd to have, in the first row, an image. What's the problem? that when the user scrolls down the tableview and, after, he scrolls up, over the image there are the informations of the other rows! Do you know why?

This is my code (i'm using a 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;
}

Thanks!

like image 676
JAA Avatar asked Dec 14 '25 13:12

JAA


1 Answers

When cells are scrolled, they are reused.

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

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

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

If indexPath.row >1, create/dequeue using CellID2, configure this and return.

If you want to keep using single cellID, then before configuring them, nil/reset all the content first so that previous data are removed.

like image 137
Sean S Lee Avatar answered Dec 18 '25 19:12

Sean S Lee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!