2012-09-23 35 views
0

代碼工作正常,直到我改變UIViewControllertableViewUICollectionViewControllerUICollectionViewCell imageView

現在所有單元格都顯示相同的圖像,有時會翻轉爲零。 但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 

回答

1

解決辦法是,每次以抵消圖像中cellForItemAtIndexPath:

cell.imageView.image = nil; 
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 
+0

我提供了單元格的代碼,但是我在原型單元格的故事板中添加了所有單元格的子視圖... – Shmidt

1

如果你正在做定製在你的畫,你需要將小區通知爲「重繪」本身時,細胞已「重用」。這可能是通過在您的UICollectionViewCell子類中實現prepareForReuse方法來完成的。

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

從技術上講,我們可能並不需要包括[super prepareForReuse]作爲文檔建議「此方法的默認實現不執行任何操作」。然而,這是一個很好的做法,誰知道蘋果是否會在未來改變默認實現prepareForReuse