子类化的 UICollectionViewCell 不更新图像



我正在尝试通过在每个单元格视图中添加一个徽章来使集合视图显示多个选择。它正确设置初始(未选择)图像,但视图永远不会更新到所选版本。我尝试手动设置它,所以我知道所选图像有效并且在单元格中可见。NSLog 显示"选定"正在按预期切换,断点显示正在执行对映像的适当分配。请原谅缩进,SO不会很好地播放,而且是在午夜之后。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
SMListCollectionViewCell *cell = (SMListCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
if ( !indexPath.row )
{
cell.image.image = [UIImage imageNamed:@"Add"];
cell.label.text = @"Create New";
cell.imageBadge.hidden = YES;
}
else
{
cell.image.image = [UIImage imageNamed:[listManager.myLists[indexPath.row-1] valueForKey:keyDetailImage]];
cell.label.text = [listManager.myLists[indexPath.row-1] valueForKey:keyName];
cell.imageBadge.hidden = !self.editing;
NSLog(@"selected is %d",cell.selected);
if ( cell.selected )
{
cell.imageBadge.image = self.badgeSelected;
}
else
{
cell.imageBadge.image = self.badgeUnselected;
}
}
return cell;
}

哇!cellForAtIndexPath:内部检查cellselected状态保证不起作用

由于您已经创建了一个自定义单元格子类SMListCollectionViewCell,因此您需要覆盖其selected资源库并从那里切换要在单元格上显示的图像。

由于imageBadge属性已经在SMListCollectionViewCell.h中公开,因此仅包含.m的快速片段如下所示:

// SMListCollectionViewCell.m
#import "SMListCollectionViewCell.h"
@implementation SMListCollectionViewCell
... 
- (void)setSelected:(BOOL)selected{
[super setSelected:selected];
if (selected){
cell.imageBadge.image = self.badgeSelected;
}
else {
cell.imageBadge.image = self.badgeUnselected;
}
}
@end

这将根据单元格选择状态处理图像的切换。

此外,cellForItemAtIndexPath:现在将变为:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
SMListCollectionViewCell *cell = (SMListCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
if (!indexPath.row){
cell.image.image = [UIImage imageNamed:@"Add"];
cell.label.text = @"Create New";
cell.imageBadge.hidden = YES;
}
else {
cell.image.image = [UIImage imageNamed:[listManager.myLists[indexPath.row-1] valueForKey:keyDetailImage]];
cell.label.text = [listManager.myLists[indexPath.row-1] valueForKey:keyName];
cell.imageBadge.hidden = !self.editing;
}
return cell;
}

相关内容

  • 没有找到相关文章

最新更新