Reputation: 4551
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
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
Reputation: 52565
You have two types of cell but only one reuse identifier.
Two options:
Upvotes: 1