首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >UICollectionViewCell imageView

UICollectionViewCell imageView
EN

Stack Overflow用户
提问于 2012-09-24 05:34:37
回答 3查看 8K关注 0票数 0

代码运行良好,直到我将UIViewControllertableView改为UICollectionViewController

现在所有单元格都显示相同的图像,有时会翻转为零。然而,textLabels是可以的。

代码语言:javascript
运行
复制
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"TourGridCell";
    TourGridCell *cell = (TourGridCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    Guide *guideRecord = [self.fetchedResultsController objectAtIndexPath:indexPath];

    cell.titleLabel.text = [guideRecord.name uppercaseString];

    cell.titleLabel.backgroundColor = [UIColor clearColor];
    if ([guideRecord.sights count] > 0) {

        if ([[guideRecord.sights objectAtIndex:0]valueForKey:@"thumbnail"]) {
            cell.imageView.image = [UIImage drawImage:[[guideRecord.sights objectAtIndex:0]valueForKey:@"thumbnail"] inImage:[UIImage imageNamed:@"MultiplePhotos"] inRect:CGRectMake(11, 11, 63, 63)];

        }else {
            cell.imageView.image = [UIImage imageNamed:@"placeholder2"];
        }

        NSMutableString *sightsSummary = [NSMutableString stringWithCapacity:[guideRecord.sights count]];
        for (Sight *sight in guideRecord.sights) {
            if ([sight.name length]) {
                if ([sightsSummary length]) {
                    [sightsSummary appendString:@", "];
                }
                [sightsSummary appendString:sight.name];

            }
        }
        if ([sightsSummary length]) {
            [sightsSummary appendString:@"."];
        }
        cell.sightsTextLabel.text = sightsSummary;
        cell.detailTextLabel.text = [NSString stringWithFormat:NSLocalizedString(@"%i", nil) , [guideRecord.sights count]];
        cell.detailTextLabel.hidden = NO;
        //        cell.textLabel.alpha = 0.7;
        NSPredicate *enabledSightPredicate = [NSPredicate predicateWithFormat:@"notify == YES"];
        NSArray *sightsEnabled = [[[guideRecord.sights array] filteredArrayUsingPredicate:enabledSightPredicate]mutableCopy];
        NSPredicate *visitedSightPredicate = [NSPredicate predicateWithFormat:@"visited == YES"];
        NSArray *sightsVisited = [[[guideRecord.sights array] filteredArrayUsingPredicate:visitedSightPredicate]mutableCopy];

        if ([sightsEnabled count] > 0)
        {
            NSLog(@"green_badge");
            cell.notifyIV.image = [UIImage imageNamed:@"green_badge"];
        }
        else if (sightsVisited.count == 0) {
            NSLog(@"new_badge");
            cell.notifyIV.image = [UIImage imageNamed:@"new_badge"];
        }
        else
        {
            cell.notifyIV.image = nil;
        }

    }
    else {
        cell.notifyIV.hidden = YES;
        //        cell.textLabel.textColor = RGB(0, 50, 140);
        cell.detailTextLabel.hidden = YES;
        cell.sightsTextLabel.text = nil;
    }

    return cell;
}

在故事板中设置单元格,

代码语言:javascript
运行
复制
//  TourGridCell.h
#import <UIKit/UIKit.h>

@interface TourGridCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UIImageView *notifyIV;
@property (weak, nonatomic) IBOutlet UILabel *textLabel;
@property (weak, nonatomic) IBOutlet UILabel *detailTextLabel;
@property (weak, nonatomic) IBOutlet UILabel *sightsTextLabel;

@end


#import "TourGridCell.h"

@implementation TourGridCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-09-24 20:36:37

解决方案是在cellForItemAtIndexPath:中每次都将图像置为空

代码语言:javascript
运行
复制
cell.imageView.image = nil;
票数 1
EN

Stack Overflow用户

发布于 2012-09-24 16:51:55

您可能需要提供更多数据,以便其他人能够找出问题所在。我想你已经检查过了,在不同的时间,条件被触发,所以不同的图像实际上被实例化并添加到你的UICollectionViewCell中?另外,我想您知道UICollectionViewCell没有图像属性,所以您需要在它的子类中添加一个属性(如果您想要方便地访问视图,而不是使用它的标签ID检索它的开销),并确保视图被添加为一个子视图?

我认为,考虑到您所描述的症状,如果您不将子视图添加到UICollectionViewCell的contentView中,就会出现这样的问题。苹果的集合视图进行了各种优化,以确保高性能,只有UICollectionViewCell的内容视图(本身是UICollectionViewCell的子视图)的子视图将按照您的预期一致绘制。

尽管根据您提供的信息,我不能确定这是否是问题的原因。

代码语言:javascript
运行
复制
@implementation UICollectionViewCellSubView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        _imageView = [[UIImageView alloc] initWithFrame:frame];
        [self.contentView addSubview:_imageView];
    }
    return self;
}


- (void)prepareForReuse
{
    [super prepareForReuse];
    // ensure you do appropriate things
    // here for when the cells are recycled
    // ...

}   

@end
票数 2
EN

Stack Overflow用户

发布于 2013-05-03 12:06:57

如果你在你的单元格中进行自定义绘图,当一个单元格被“重用”时,你需要通知单元格“重绘”它自己。最简单的方法可能是在UICollectionViewCell子类中实现prepareForReuse方法。

代码语言:javascript
运行
复制
- (void)prepareForReuse {
    [super prepareForReuse];
    [self setNeedsDisplay];
}

从技术上讲,我们可能不需要包含[super prepareForReuse],因为文档建议“此方法的默认实现什么也不做”。然而,这是一个很好的实践,谁知道苹果将来会不会改变prepareForReuse的默认实现。

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

https://stackoverflow.com/questions/12556470

复制
相关文章

相似问题

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