Ios UICollectionView重新加载数据问题-单元格的子视图问题
我有以下看法 但是当调用-reloadData时,视图变成 我想这是因为细胞被重复利用了。 即使调用重载数据,您也可以将视图保留为第一个图像吗 CustomCell.mIos UICollectionView重新加载数据问题-单元格的子视图问题,ios,objective-c,uicollectionview,Ios,Objective C,Uicollectionview,我有以下看法 但是当调用-reloadData时,视图变成 我想这是因为细胞被重复利用了。 即使调用重载数据,您也可以将视图保留为第一个图像吗 CustomCell.m @property (nonatomic, strong) UIImageView* imageView; @property (nonatomic, strong) UILabel* label; //... - (id)initWithFrame:(CGRect)frame { self = [super ini
@property (nonatomic, strong) UIImageView* imageView;
@property (nonatomic, strong) UILabel* label;
//...
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.imageView = [[UIImageView alloc]initWithFrame:imgViewFrame];
[self.contentView addSubview:self.imageView];
self.label = [[UILabel alloc]initWithFrame:labelFrame];
[self.contentView addSubview:self.label];
}
return self;
}
- (void)prepareForReuse
{
//Set default values here
}
编辑:
我修改了代码。
-在使用正确的数据进行设置之前,先设置所有默认值
-调用-准备就绪
调用-reloadData时,尽管imageView.image保持为nil,但indexath.item==0中的label.text变为nil,indexPath.item==3输出@Hello 是的,这是因为可重用性。您可以通过将代码行更改为以下内容来避免:
if (indexPath.item == 0)
{
cell.label.text = @"Hello";
cell.imageView.image = nil;
}
else
{
UIImage *image = dataArray[indexPath.item];
cell.imageView.image = image;
cell.label.text = @"";
}
当您检索出列单元时,它们已经设置好了。因此,第一次检索9个不同的单元格并相应地初始化它们。 第二次,单元格属性已设置,因此需要重新启动它们。 因此,您的代码应该如下所示:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellIdentifier forIndexPath:indexPath];
if (indexPath.row == 0)
{
cell.imageView.image = nil;
cell.label.text = @"Hello";
}
else
{
UIImage *image = dataArray[indexPath.item];
cell.imageView.image = image;
cell.label.text = nil;
}
return cell;
}
在执行任何检查之前,请尝试设置单元格的所有默认值:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellIdentifier forIndexPath:indexPath];
//Set all default values before setting them with correct data
cell.imageView.image = nil;
cell.label.text = @"";
if (indexPath.row == 0)
{
cell.label.text = @"Hello";
}
else
{
UIImage *image = dataArray[indexPath.item];
cell.imageView.image = image;
}
return cell;
}
编辑:
您可以将此方法添加到CustomCell.m
@property (nonatomic, strong) UIImageView* imageView;
@property (nonatomic, strong) UILabel* label;
//...
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.imageView = [[UIImageView alloc]initWithFrame:imgViewFrame];
[self.contentView addSubview:self.imageView];
self.label = [[UILabel alloc]initWithFrame:labelFrame];
[self.contentView addSubview:self.label];
}
return self;
}
- (void)prepareForReuse
{
//Set default values here
}
报告:
我自己解决了这个问题,所以我会报告的。
我把房子藏起来了
我想是因为可重用的电池。。在标签和图像视图上设置文本和图像之前,使用ReuseIdentifier将ReuseAbleCellWithReuseIdentifier将其设置为cell.label.text=@;cell.imageview.image=nil;然后设置要设置的内容修改代码后结果是否相同?在调用-reloadData时,尽管imageView.image保持为nil,但indexPath中的label.text.item==0变为nil,indexPath中的label.text.item==3 outputs@Hello.it不应为indexPath.item,但indexPath.Row这看起来不完整。在执行任何其他操作之前,应将隐藏属性默认设置为“否”