我在我的應用程序中使用了collectionView。我在didSelect委託中設置單元格backgroundView的圖像。但是,當我選擇一個單元格索引路徑圖像正在設置爲3單元格索引路徑。當我滾動collectionView圖像隨機變化?請幫幫我。提前致謝。UICollectionView - 圖像隨機設置
- (void)viewDidLoad
{
[super viewDidLoad];
[collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:uio];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection: (NSInteger)section
{
return 50;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collection dequeueReusableCellWithReuseIdentifier:uio
forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"index %@",indexPath);
UICollectionViewCell *cell = [collection cellForItemAtIndexPath:indexPath];
cell.backgroundView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"download.jpg"]];
}
請附上您的代碼 – dKrawczyk
請出示您的cellForItemAtIndexPath()方法 –
沒有看到你的代碼很難告訴,但我的猜測是,單元格正在重新使用,當你滾動。 – jervine10