UICollectionView reloadData问题 - 单元的子视图问题

时间:2015-05-12 10:39:11

标签: ios objective-c uicollectionview

我已经提出了以下观点。

enter image description here

但是当调用-reloadData时,视图变为......

enter image description here

我认为这是因为细胞被重复使用 即使致电reloadData,您是否有任何想法将视图保持为第一张图像?

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellIdentifier forIndexPath:indexPath];

    if (indexPath.item == 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;
}

编辑:

我修改了我的代码   - 在使用正确的数据设置之前设置所有默认值
  - 调用-prepareForReuse
调用-reloadData时,虽然imageView.image保持为零,但indexPath.item == 0中的label.text变为nil,而indexPath.item中的label.text == 3输出@"你好&#34 ;.

4 个答案:

答案 0 :(得分:0)

是的,因为可重用性。你可以通过改变代码行来避免它:

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 = @"";
}

答案 1 :(得分:0)

当您检索出列的单元格时,它们已经设置好了。因此,您第一次检索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;
}

答案 2 :(得分:0)

在进行任何检查之前,请尝试设置单元格的所有默认值:

- (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

- (void)prepareForReuse
{
    //Set default values here
}

答案 3 :(得分:0)

报告:
我自己解决了这个问题。所以,我会报告。
我使用了属性hidden

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellIdentifier forIndexPath:indexPath];

    if (indexPath.item == 0)
    {
        cell.label.text = @"Hello";

        cell.imageView.hidden = YES;
    }
    else
    {
        UIImage *image = dataArray[indexPath.item];
        cell.imageView.image = image;

        cell.label.hidden = YES;
    }

    return cell;
}