に問題がありますUICollectionView
。最初は正常に表示され、セルのグリッドが表示され、各セルには単一のUIImageView
. これらUIImageViews
は、アプリのバンドルに保存されている透明な PNG を示しています。
私の問題は、UICollectionView
スクロールすると、一部のセルが破損しているように見えることです。
破損したセルは複数の画像が重なり合って表示されます。一番上の画像が表示されるべき画像であり、その下の画像は他のセルで使用されるべき画像です。
私の最善の推測では、これは a 内のセルが再利用される方法と関係があるということですUICollectionView
が、私は提案を受け付けています。
これは、内にセルを作成するために使用するデリゲート コードですUICollectionView
。
// creates the individual cells to go in the menu view
- (UICollectionViewCell*) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
// create collection view cell
UICollectionViewCell * cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
// create a uiview where we can place all views that need to go into this cell
UIView * contents=[[UIView alloc] initWithFrame:cell.contentView.bounds];
[contents setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubview:contents];
// add a button image
NSString * buttonPath=[[NSBundle mainBundle] pathForResource:@"button" ofType:@"png" inDirectory:[[buttons objectAtIndex:indexPath.row] objectForKey:@"name"]];
UIImage * button=[UIImage imageWithContentsOfFile:buttonPath];
UIImageView * buttonView=[[UIImageView alloc] initWithImage:button];
[buttonView setContentMode:UIViewContentModeScaleAspectFit];
[buttonView setFrame:contents.bounds];
[contents addSubview:buttonView];
// set tag to the indexPath.row so we can access it later
[cell setTag:indexPath.row];
// add interactivity
UITapGestureRecognizer * tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onButtonTapped:)];
[tap setNumberOfTapsRequired:1];
[cell addGestureRecognizer:tap];
// return the cell
return cell;
}
必要に応じて、さらにコードを提供できます。
セルの破損を防ぐにはどうすればよいですか?