0

私のUICollectionViewCellにはストーリーボードのボタンがあり、ボタンの数の背景イメージをドキュメントディレクトリから動的に設定することができます。そのコントローラを再度ロードすると、問題は依然として存在します。どのようにしてこの問題を解決できますか?マイCollectionViewControllerで再利用可能なUICollectionViewCellはリフレッシュしません

:私のカスタム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; 
} 

@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

たぶん問題がありますその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

関連する問題