首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CALayer addSublayer在didSelectItemAtIndexPath中不起作用

CALayer addSublayer在didSelectItemAtIndexPath中不起作用
EN

Stack Overflow用户
提问于 2021-01-05 15:57:46
回答 1查看 30关注 0票数 0

我想实现在CollectionView中单击单元格时可以变成红色的效果,所以我使用了CAlayer,但它不起作用。当我使用target-action在单元内部实现时,它可以完美地工作。代码如下:

代码语言:javascript
复制
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    videoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"videoCell" forIndexPath:indexPath];
    CALayer *testLayer = [[CALayer layer] init];
    testLayer.frame = cell.bounds;
    testLayer.backgroundColor = [UIColor redColor].CGColor;
    [cell.layer addSublayer:testLayer];
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-05 16:49:26

您需要使用单元的contentView来完成此操作。此外,请记住,单元格是缓存,因此您不能使用存储在单元格中的状态。你得把它放在别的地方。并且您不应该使用didSelectItemAtIndexPath来更新UI。而是更改那里的状态,然后请求并更新相关单元。

这里有一个很好的例子来说明。在此示例中,您可以选择多个单元格,它们都将具有红色背景。单元格的选择状态存储在控制器的字典中。如果你只想在给定的时刻选择一个单元格,你可以很容易地改变这一点。然后,您还需要更新变为未选中的单元格,但这是另一个示例。

代码语言:javascript
复制
#import "CollectionViewController.h"

@interface CollectionViewController () < UICollectionViewDelegate, UICollectionViewDataSource >

@property (nonatomic,strong) NSMutableDictionary * selectedCells; // Key is integer row, value is boolean selected

@end

@implementation CollectionViewController

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Register cell classes
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    // Do any additional setup after loading the view.
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;

    // Empty / nothing selected for now
    self.selectedCells = NSMutableDictionary.dictionary;
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 5;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    // Configure the cell based on state
    if ( [( NSNumber * )[self.selectedCells objectForKey:@( indexPath.row )] boolValue] ) {
        cell.contentView.backgroundColor = UIColor.redColor;
    } else {
        cell.contentView.backgroundColor = UIColor.blueColor;
    }
    
    return cell;
}

#pragma mark <UICollectionViewDelegate>

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    // Flip selected state
    [self.selectedCells setObject:@( ! [( NSNumber * )[self.selectedCells objectForKey:@( indexPath.row )] boolValue] )
                   forKey:@( indexPath.row )];

    // Request an UI update to reflect the updated state
    [collectionView reloadItemsAtIndexPaths:@[ indexPath ]];
}

@end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65575003

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档