代码正常工作,直到我将UIViewController
更改为tableView
到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
答案 0 :(得分:2)
您可能需要提供更多数据,以便其他人可以解决问题所在。我认为你已经检查过,在不同的时间这被称为条件被触发,所以不同的图像实际上被实例化并添加到你的UICollectionViewCell?另外我认为你知道UICollectionViewCell没有图像属性,所以你需要在它的子类中添加一个属性(如果你想轻松访问视图而不需要使用它来检索它的开销) #39; s标签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
答案 1 :(得分:1)
解决方案是每次在cellForItemAtIndexPath:
cell.imageView.image = nil;
答案 2 :(得分:1)
如果您在单元格中进行自定义绘图,则需要在单元格被“重用”时通知单元格“重绘”。通过在UICollectionViewCell子类中实现prepareForReuse
方法,这可能是最简单的方法。
- (void)prepareForReuse {
[super prepareForReuse];
[self setNeedsDisplay];
}
从技术上讲,我们可能不需要包含[super prepareForReuse]
,因为文档建议'此方法的默认实现不执行任何操作'。但是,这是一个很好的做法,谁知道Apple将来是否会更改prepareForReuse
的默认实现。