gpt4 book ai didi

ios - 在 didSelectItemAtIndexPath 中更改 UICollectionViewCell 的属性

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:47:51 25 4
gpt4 key购买 nike

我在子类中配置一个 UICollectionViewCell,它向 contentView 属性添加了 2 个 subview ,都是 UIImageView 并且都有 hidden 属性设置为 YES。这些 subview 是“已选中”和“未选中”的图像,它们覆盖单元格中的主 UIImageView,以指示当前单元格是否使用 UICollectionView 的“多选”功能被选中。

点击单元格时,将在委托(delegate)上调用 collectionView:didSelectItemAtIndexPath:,我想在“选中的”UIImageView 上设置 setHidden:NO。在单元格上调用它根本没有任何作用——单元格似乎锁定在其最初绘制的状态。

是否可以对 collectionView:cellForItemAtIndexPath: 之外的单元格进行更改?我尝试在 collectionView:didSelectItemAtIndexPath: 中手动添加 subview ,但它对 UI 完全没有任何改变。我已验证委托(delegate)方法已被调用,只是没有更改我的单元格。

- (void) collectionView(UICollectionView *)cv didSelectItemAtIndexPath(NSIndexPath *)indexPath {
ShotCell *cell = [self collectionView:cv cellForItemAtIndexPath:indexPath];
UILabel *testLabel = UILabel.alloc.init;
testLabel.text = @"FooBar";
testLabel.sizeToFit;
[cell.contentView.addSubview testLabel];
}

最佳答案

您尝试执行此操作的方式不正确。您需要在属性中保留对所选单元格的引用。在此示例中,我使用一个数组来保存所选单元格的索引路径,然后检查传递给 cellForItemAtIndexPath 的索引路径是否包含在该数组中。如果您单击已选择的单元格,我将取消选择该单元格:

@interface ViewController ()
@property (strong,nonatomic) NSArray *theData;
@property (strong,nonatomic) NSMutableArray *paths;
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
self.paths = [NSMutableArray new];
self.theData = @[@"One",@"Two",@"Three",@"Four",@"Five",@"Six",@"Seven",@"Eight"];
[self.collectionView registerNib:[UINib nibWithNibName:@"CVCell" bundle:nil] forCellWithReuseIdentifier:@"cvCell"];

UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
[self.collectionView setCollectionViewLayout:flowLayout];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.theData.count;
}

-(CVCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"cvCell";
CVCell *cell = (CVCell *) [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
cell.label.text = self.theData[indexPath.row];
if ([self.paths containsObject:indexPath]) {
[cell.iv setHidden:NO]; // iv is an IBOutlet to an image view in the custom cell
}else{
[cell.iv setHidden:YES];
}
return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
if ([self.paths containsObject:indexPath]) {
[self.paths removeObject:indexPath];
}else{
[self.paths addObject:indexPath];
}

[self.collectionView reloadItemsAtIndexPaths:@[indexPath]];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

return CGSizeMake(150, 150);
}

关于ios - 在 didSelectItemAtIndexPath 中更改 UICollectionViewCell 的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18094449/

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