gpt4 book ai didi

ios - UICollectionView reloadData 有效,但 reloadItemsAtindexPaths 无效

转载 作者:行者123 更新时间:2023-11-29 01:41:59 29 4
gpt4 key购买 nike

我尝试了非常简单的事情:在按下按钮时在 UICollectionViewCell 的 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];
}

最佳答案

_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]];

}

关于ios - UICollectionView reloadData 有效,但 reloadItemsAtindexPaths 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32296183/

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