gpt4 book ai didi

objective-c - UITableView 和自定义单元格

转载 作者:行者123 更新时间:2023-11-29 04:44:47 25 4
gpt4 key购买 nike

我有一个带有自定义单元格的tableView,我的第一个单元格与其他单元格不同。 (它包含一个小图像和一个标签)。我正在这样做:

#

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

第一次很好,但是当我滚动我的第一个单元格(添加的小图像)时,它出现在其他行中。我做错了什么?谢谢

最佳答案

您有两种类型的单元格,但只有一个重用标识符。

两种选择:

  • 确保当您的单元格被重复使用时,它会将其状态重置为默认的空状态。有一个用于此目的的prepareForReuse方法
  • 定义两个不同的单元,每个单元都有自己唯一的重用标识符。这样您的第一个不同的单元以后就不会被重复使用

关于objective-c - UITableView 和自定义单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9807325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com