0

내 UICollectionViewCell에 스토리 보드의 버튼이 있고 문서 디렉토리에서 버튼의 배경 이미지를 동적으로 설정하는 경우 일부 버튼과 같은 문제가 발생합니다. 배경 이미지가 상쾌하지 않고 이미지를 닫을 때도 오래된 이미지를 유지합니다. 컨트롤러를 다시로드하고 해당 컨트롤러를 다시로드하면 문제가 계속 발생합니다. 이 문제를 어떻게 해결할 수 있습니까?재사용 가능한 UICollectionViewCell이 새로 고침되지 않습니다.

@interface CustomCollectionViewCell : UICollectionViewCell 

@property (weak, nonatomic) IBOutlet UIButton *cellButton; 

@end 

@implementation CustomCollectionViewCell 

-(void)prepareForReuse { 
    NSLog(@"Is it possible to clear here ? but this method didn't call in my code"); 
} 

@end 
+0

wt는 섹션 및 행에 대한 데이터 소스입니까? – kaushal

+0

plist 파일에서 데이터를 가져 오는 중입니다. – Nuibb

답변

2

어쩌면 문제는 내 사용자 지정 collectionView 셀에서

- (NSString *)getBackgroundImage:(NSIndexPath *)indexPath { 

    NSArray *indexes = [self.allColors[indexPath.section] objectForKey:@"indexes"]; 

    return [NSString stringWithFormat:@"%@/%@/%@/combimeter_button/combimeter_%ld_%@@2x.png", [self.documentDirectory getDocumentsDirectory], _selectedCar, _selectedCar, (long)[indexes[indexPath.row] integerValue], [self getColorKey:indexPath.section]]; 
} 


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 

    CustomCollectionViewCell *cell = (CustomCollectionViewCell *) [collectionView dequeueReusableCellWithReuseIdentifier:@"CustomCell" forIndexPath:indexPath]; 

    cell.layer.borderWidth = 2.0f; 
    cell.layer.borderColor = [[self getBackgroundColor:indexPath.section] CGColor]; 
    [cell.cellButton setBackgroundImage:[UIImage imageNamed:[self getBackgroundImage:indexPath]] forState:UIControlStateNormal]; 
    [cell.cellButton addTarget:self action:@selector(combimeterButtonDidSelected:) forControlEvents:UIControlEventTouchUpInside]; 

    return cell; 
} 

: 내 CollectionViewController에서

imageNamed: 이미지를 캐시합니다. 대신 imageWithContentsOfFile:을 사용해보세요.

+0

감사합니다 junk_key, 내 생명을 구했어 ... :) – Nuibb

0

새로운 뷰를 작성하기 전에 이전의 모든 뷰를 제거하십시오 :

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 

    CustomCollectionViewCell *cell = (CustomCollectionViewCell *) [collectionView dequeueReusableCellWithReuseIdentifier:@"CustomCell" forIndexPath:indexPath]; 

    for (UIView *subview in [cell subviews]) { 
     [subview removeFromSuperview]; 
    } 

    ... 
} 
+0

아니요,이 기능이 작동하지 않습니다.이 경우 모든 버튼이 비어 있고 배경 이미지가 표시되지 않습니다. – Nuibb