【问题标题】:Sub-classed UICollectionViewCell not updating image子类 UICollectionViewCell 不更新图像
【发布时间】:2017-02-18 00:40:03
【问题描述】:

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

- (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;
}

【问题讨论】:

  • 很难做到这一点。不要忘记单元格一直在变化和重绘。您必须“选择”在您的数据中。因此,如果您有一系列“汽车”,例如其中 10 辆,则选择“3”。您必须在汽车中有一个字段,并且在第三个中,您必须将该汽车设置为“已选择”。那么在tableView#cellForRowAt 中绘制单元格时必须使用该质量
  • @JoeBlow 告诉我你对我在下面的answers中提出的方法的看法。

标签: ios objective-c cocoa uicollectionview


【解决方案1】:

哇!cellForAtIndexPath: 中检查cellselected 状态保证不会工作。

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

由于imageBadge 属性已经在SMListCollectionViewCell.h 中公开,因此只有.m 的快速sn-p 看起来像:

// 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;
}

【讨论】:

  • @josh-caswell 感谢您的编辑,但我认为表情符号很有趣! :) 哈哈哈
  • 我们在这里并没有真正为他们服务。不过答案很好。
  • 很高兴你喜欢它。 ?
  • 谢谢!复制'n'粘贴你的答案,它工作得很好——除了需要在我编辑的 setSelected 中调整对'cell'和'self'的引用。
猜你喜欢
  • 1970-01-01
  • 2012-05-08
  • 2014-11-18
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-23
  • 1970-01-01
相关资源
最近更新 更多