iOS で UICollectionview をリロードしようとすると問題が発生します。
ゲームのレベル表示に使用しています。
コレクションビューは 10 個のセルで構成されています。セルの内容は、レベルがロック解除されているかどうかによって異なります。レベルのロックが解除されている場合、セルはレベル (カスタム UIView) を表示し、それ以外の場合は画像を表示します。これを機能させるには、個々のセル識別子を作成する必要があり、ロード時にすべてが完全に表示されます。
私の問題は、ユーザーがロックされていないレベルをプレイしていて、次のレベルのロックを解除したときです。ユーザーがゲーム ビューからレベル選択ビューに戻ると、セルが正しくリロードされません (カスタム ビューがあるべき場所に空白が表示され、画像が正しく表示されます)。
viewWillAppear のレベルで配列をアンロードしてから [collectionview reloadData]; を呼び出し、レベルをロードしてコレクションビューを再度リロードしようとしましたが、それは役に立ちません。
コレクションビュー全体を空にして、ビューが表示されたときにセル識別子を再作成するにはどうすればよいですか?
ありがとう
-編集!コードで更新 -
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
levelsArray = nil;
puzzlesArray = nil;
levelsArray = [[NSMutableArray alloc]init];
puzzlesArray = [[NSMutableArray alloc]init];
[collectionView reloadData];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [defaults objectForKey:@"puzzles"];
puzzlesArray = [NSKeyedUnarchiver unarchiveObjectWithData:data];
if ([puzzlesArray count] == 0) {
[self.navigationController popViewControllerAnimated:YES];
}
else {
NSLog(@"%i puzzles loaded", [puzzlesArray count]);
//Get alle puzzles for the current category
for (Puzzle *puzzle in puzzlesArray) {
if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"Category"] isEqualToString:[puzzle categoryName]]) {
[levelsArray addObject:puzzle];
}
}
}
NSLog(@"View will appear");
[collectionView reloadData];
}
そして、インデックスパスのアイテムのセル
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath; {
BOOL isUnlocked = [self isPuzzleUnlocked:[indexPath row]];
if ([[NSUserDefaults standardUserDefaults]boolForKey:@"u"] == YES) {
isUnlocked = YES;
}
[self.collectionView registerNib:[UINib nibWithNibName:@"CVCell" bundle:nil] forCellWithReuseIdentifier:[NSString stringWithFormat:@"%@%d", kCellReuseIdentifier, indexPath.row]];
CVCell *cell = (CVCell *)[collectionView dequeueReusableCellWithReuseIdentifier:[NSString stringWithFormat:@"%@%d", kCellReuseIdentifier, indexPath.row] forIndexPath:indexPath];
[cell setPuzzleInfo:[levelsArray objectAtIndex:[indexPath row]] unlocked:isUnlocked];
return cell;
}