samir
samir

Reputation: 4551

UITableView and custom cells

i have a tableView with a custom cell, my first cell is different with the others cells. ( it contain a small image and a label). I am doing like this :

#

pragma mark - tableView data source methods
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{


    SuggestionCarteCell *cell = (SuggestionCarteCell *)[tableView dequeueReusableCellWithIdentifier:@"carteCell"];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SuggestionCarteCell" owner:nil options:nil] ;
        cell = [nib objectAtIndex:0];
    }

    if (indexPath.row == 0) {
        // crer l'image

        NSLog(@" je suis dans row 0");
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(21, 9, 13, 26)];
        imageView.image = [UIImage imageNamed:@"iconeLocation"];
        [cell addSubview:imageView];
        cell.labelRegion.frame = CGRectMake(21+13+10, cell.labelRegion.frame.origin.y,cell.labelRegion.frame.size.width , cell.labelRegion.frame.size.height);
        cell.labelRegion.text = [NSString stringWithFormat:@"A proximité,"];

    }
    else{
    Region *region = [Region new];
    region  =(Region *) [arrayRegion objectAtIndex:indexPath.row];
    cell.labelRegion.text =region.nomRegion;


    }
    return cell;
}

in the first time it's good, but when i scroll my first cell ( the small image added) appear in the other rows. What i am doing wrong ? thanks

Upvotes: 0

Views: 320

Answers (2)

Demz
Demz

Reputation: 1058

Try like this

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


SuggestionCarteCell *cell = (SuggestionCarteCell *)[tableView dequeueReusableCellWithIdentifier:@"carteCell"];
 UIImageView *imageView;
 if (cell == nil) {
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SuggestionCarteCell" owner:nil options:nil] ;
    cell = [nib objectAtIndex:0];
    imageView = [[UIImageView alloc]initWithFrame:CGRectMake(21, 9, 13, 26)];
    imageView.tag = 100;
    [cell addSubview:imageView];
} else {
    imageView = [cell viewWithTag:100];
}

if (indexPath.row == 0) {
    // crer l'image

    NSLog(@" je suis dans row 0");

    imageView.image = [UIImage imageNamed:@"iconeLocation"];

    cell.labelRegion.frame = CGRectMake(21+13+10, cell.labelRegion.frame.origin.y,cell.labelRegion.frame.size.width , cell.labelRegion.frame.size.height);
    cell.labelRegion.text = [NSString stringWithFormat:@"A proximité,"];

}
else{
    Region *region = [Region new];
    region  =(Region *) [arrayRegion objectAtIndex:indexPath.row];
    cell.labelRegion.text =region.nomRegion;
    imageView.image = nil;
}
return cell;

}

This way, the cells are reused, the image view is created only once, then you get its reference by its tag, but in one case you set its image, in the other case, you set it to nil.

Upvotes: 0

Stephen Darlington
Stephen Darlington

Reputation: 52565

You have two types of cell but only one reuse identifier.

Two options:

  • Make sure that when your cell is reused it resets its state to a default, empty state. There's a prepareForReuse method for this purpose
  • Define two different cells, each with their own, unique reuse identifier. That way your first, different cell will not get reused later on

Upvotes: 1

Related Questions