2012-09-23 8 views
0

UIViewControllertableView에서 UICollectionViewController으로 바꿀 때까지 코드가 정상적으로 작동합니다.UICollectionViewCell 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을 바꿀 것인지를 아는 사람입니다.