子类UICollectionViewCell没有更新的图像

问题描述:

我试图通过在每个单元视图中添加一个徽章,使集合视图显示多个选择。它正确设置了初始(未选定)图像,但视图从未更新为选定版本。我试着手动设置它,所以我知道所选图像的作品,并在单元格中可见。 NSLog显示'selected'正在按预期切换,断点显示正在执行对图像的适当分配。不好意思,SO不会打得很好,而且在午夜之后。子类UICollectionViewCell没有更新的图像

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

这是令人难以置信很难做到这一点。不要忘记**单元格正在改变,并一直在重绘**。你必须在数据中“选择”***。所以如果你有一组“汽车”,说他们10,并选择“3”。你必须在汽车领域拥有一个领域,而在第三方面,你必须将汽车设置为“选定”*。那么你有当你画的细胞'的tableView#cellForRowAt' – Fattie

+0

@JoeBlow让我知道你怎么想,我在下面_answers_提出的方法使用该质量。 – ystack

哇!从内部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; 
} 
+0

@ josh-caswell感谢编辑,但我认为emojis很有趣! :) haha​​ha – ystack

+0

我们并不真正为他们在这里附近。好的答案,但。 –

+0

很高兴你喜欢它。 – ystack