У меня проблема с моим UICollectionView
. Первоначально он отображает тонкий, показывая сетку ячеек, каждая ячейка с одним UIImageView
. Эти UIImageViews
показывают PNG с прозрачностью, которые хранятся в комплекте приложений.
Моя проблема в том, что после прокрутки UICollectionView
некоторые из ячеек кажутся поврежденными.
Коррумпированная ячейка показывает несколько изображений, уложенных друг на друга, верхнее большинство изображений - это то, которое должно отображаться, а изображения под ними - те, которые должны использоваться в других ячейках.
Мое лучшее предположение заключается в том, что это связано с тем, как ячейки внутри 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;
}
Я могу предоставить больше кода, если это необходимо.
Как я могу остановить повреждение ячеек?