Ios 如何更改UICollectionViewCell';细胞外的背景
如何在单元格外部更改iOS UICollectionViewCell背景 例如,我有一个视图控制器,它实现了Ios 如何更改UICollectionViewCell';细胞外的背景,ios,objective-c,uicollectionview,uicollectionviewcell,Ios,Objective C,Uicollectionview,Uicollectionviewcell,如何在单元格外部更改iOS UICollectionViewCell背景 例如,我有一个视图控制器,它实现了UICollectionViewDelegate方法: - (void)viewDidLoad { [super viewDidLoad]; [self.collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:MY_CELL_REUSE_IDENTIFIER]
UICollectionViewDelegate
方法:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:MY_CELL_REUSE_IDENTIFIER];
}
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:MY_CELL_REUSE_IDENTIFIER forIndexPath:indexPath];
if (condition)
{
[self actionOne];
cell.backgroundColor = [UIColor redColor];
}
else
{
[self actionTwo];
cell.backgroundColor = [UIColor greenColor];
}
return YES;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:MY_CELL_REUSE_IDENTIFIER forIndexPath:indexPath];
cell.backgroundColor = [UIColor orangeColor];
}
if(condition)
语句中的断点显示执行了所需的行。但是,可悲的是,细胞背景保持不变
更重要的是,我成功地做了几乎相同的事情idUICollectionViewDataSource
方法:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:MY_CELL_REUSE_IDENTIFIER];
}
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:MY_CELL_REUSE_IDENTIFIER forIndexPath:indexPath];
if (condition)
{
[self actionOne];
cell.backgroundColor = [UIColor redColor];
}
else
{
[self actionTwo];
cell.backgroundColor = [UIColor greenColor];
}
return YES;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:MY_CELL_REUSE_IDENTIFIER forIndexPath:indexPath];
cell.backgroundColor = [UIColor orangeColor];
}
我做错了什么?这应该可以做到:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
MyCollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
if (condition)
{
[self actionOne];
cell.backgroundColor = [UIColor redColor];
}
else
{
[self actionTwo];
cell.backgroundColor = [UIColor greenColor];
}
}
尝试用
cellforrowatinexpath
代替dequeueReusableCellWithReuseIdentifier
编辑:仅在中应选择ItemAtIndexPath
说明:
dequeueReusableCellWithReuseIdentifier
将为您提供一个新的单元格,您可以更改其背景颜色,但该单元格从未添加到表中,因此您不会看到它。使用cellForRowAtIndexPath
时,您将从表中获得一个要编辑的单元格。检查“单元格”是否为空。单元格不是零。你能评论一下调用actionOne/actionTwo方法的行吗?告诉我发生了什么?什么都没有发生。这是有意义的,因为这些方法独立于细胞。。。