【问题标题】:UICollectionView reloadData works, but reloadItemsAtindexPaths notUICollectionView reloadData 有效,但 reloadItemsAtindexPaths 无效
【发布时间】:2015-08-30 11:43:41
【问题描述】:

我尝试了非常简单的事情:在按下按钮时在UICollectionViewCell's imageView 上添加图像。 (实际上,CollectionViewCell - 是带有UIImageView IBOutlet 的自定义单元格)

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    self.cell=cell;
    return cell;
}

- (IBAction)actionAddImage:(UIBarButtonItem *)sender {
    UIImage* image=[UIImage imageNamed:@"1.jpg"];
   self.cell.imageView.image=image;
   NSIndexPath* path= [self.collectionView indexPathForCell:self.cell];
   [self.collectionView reloadItemsAtIndexPaths:@[path]];
}

只有一个单元格。在第一个按钮按下没有任何反应,在第二个 - 图像出现。我输入NSLog,并透露,

CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];

返回新单元格而不是现有单元格。虽然indexPath 很强烈(section=0, row=0),即只有一个细胞。 但是,reloadData 可以完美运行,但当然没有动画。

- (IBAction)actionAddImage:(UIBarButtonItem *)sender {
    UIImage* image=[UIImage imageNamed:@"1.jpg"];
   self.cell.imageView.image=image;
   [self.collectionView reloadData];
}

【问题讨论】:

    标签: ios objective-c uicollectionview uicollectionviewcell


    【解决方案1】:
    _array =[NSMutableArray arrayWithObjects:@{@"option":[NSNumber numberWithBool:NO]},@{@"option":[NSNumber numberWithBool:NO]},@{@"option":[NSNumber numberWithBool:NO]},@{@"option":[NSNumber numberWithBool:NO]}, nil];
    
    - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        questionImageCollectionViewCell * cell=[self.questionCollectionView dequeueReusableCellWithReuseIdentifier:@"coustomCell" forIndexPath:indexPath];
        cell.optionImageView.image=[UIImage imageNamed:[_optionImageArr objectAtIndex:indexPath.item]];
    
    
        NSDictionary *dic = self.array[indexPath.row];
    
        if ([dic[@"option"] boolValue]) {
            [cell.rightTickImg setHidden:NO];
        }else{
          [cell.rightTickImg setHidden:YES];
    
        }
       // [cell.rightTickImg setHidden:YES];
        return cell;
    
    
    }
    
    
    - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
    {
    
            //[self.array removeObjectAtIndex:indexPath.row];
        NSDictionary *dic = @{@"option":[NSNumber numberWithBool:YES]};
        [self.array insertObject:dic atIndex:indexPath.row];
    
        [collectionView reloadItemsAtIndexPaths:@[indexPath]];
    
    }
    

    【讨论】:

    • 使用此代码隐藏并在集合视图单元格上显示图像视图
    【解决方案2】:

    第一次,您的代码非常不安全。当您在 'collectionView:cellForItemAtIndexPath:' 中“创建它”时,您不应该分配您的可重用单元格,而不是您可以尝试这样做:

    - (IBAction)actionAddImage:(UIBarButtonItem *)sender 
    {
        NSIndexPath *path    = [NSIndexPath indexPathForItem:0 inSection:0]; // i suppose you can reload cell at section 0 position 0
        CollectionViewCell *cell = (CollectionViewCell *)[_collectionView cellForItemAtIndexPath:path];
        UIImage *image = [UIImage imageNamed:@"1.jpg"];
    
        cell.imageView.image = image;
    
        .... reload _collectionView at Path 'path' ....
    }
    

    在其他事情之间,您对可重用单元所做的这个外部引用不是在时间上不可变的。

    【讨论】:

    • 我理解你。我使用了您的代码,但没有任何改变。 reloadData 工作,[self.collectionView reloadItemsAtIndexPaths:@[path]];- 不工作。当最后一个使用时,在方法中 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath]; 单元格不同。也许是包?
    • 不,这样细胞将是相同的,你只有一个,所以必须永远相同。请调试您的代码单元格并在“actionAddImage:”中放置一个断点,然后再将“image”分配给单元格并探索单元格对象并告诉我单元格的属性“imageView”是否为 nil 或已初始化;)
    • imageView 已初始化.. UIImage* image=[UIImage imageNamed:@"1.jpg"]; NSLog(@"before reload %@",cell.imageView); cell.imageView.image=image; before reload <UIImageView: 0x7fbce9c70b90; frame = (15 6; 24 116); autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x7fbce9c6efc0>>
    猜你喜欢
    • 2014-01-31
    • 1970-01-01
    • 1970-01-01
    • 2017-01-08
    • 2012-05-08
    • 2017-03-15
    • 2014-10-30
    • 2011-03-08
    • 2013-11-27
    相关资源
    最近更新 更多