0

代码工作正常,直到我更改UIViewControllertableViewto UICollectionViewController

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

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

在故事板中设置单元格,

//  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
4

3 回答 3

2

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

我认为,鉴于您所描述的症状,如果您没有将子视图添加到 UICollectionViewCell 的 contentView,则可能会出现此类问题。Apple 的集合视图进行了各种优化以确保高性能,并且只有 UICollectionViewCell 的内容视图(它本身是 UICollectionViewCell 的子视图)的子视图将按照您的预期始终如一地绘制。

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

@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
于 2012-09-24T08:51:55.213 回答
1

解决方案是每次在cellForItemAtIndexPath:

cell.imageView.image = nil;
于 2012-09-24T12:36:37.257 回答
1

如果您在单元格中进行自定义绘图,则需要通知单元格在“重用”单元格时自行“重绘”。prepareForReuse这可能是通过在 UICollectionViewCell 子类中实现该方法最简单的方法。

- (void)prepareForReuse {
    [super prepareForReuse];
    [self setNeedsDisplay];
}

从技术上讲,我们可能不需要[super prepareForReuse]像文档建议的那样包含“此方法的默认实现什么都不做”。但是,这是一种很好的做法,谁知道苹果将来是否会更改默认实现prepareForReuse

于 2013-05-03T04:06:56.840 回答