UICollectionView - 图像随机设置 [英] UICollectionView - Image is getting set randomly
问题描述
我在我的应用程序中使用 collectionView.我正在为 didSelect 委托中的单元格背景视图设置图像.但是当我选择一个单元格 indexPath 时,图像被设置为 3 个单元格 indexPath.当我滚动 collectionView 时,图像会随机更改吗?请帮我.提前致谢.
I am using collectionView in my App. I am setting image for the cell backgroundView in didSelect delegate. But When i select one cell indexPath the image is getting set for 3 cell indexPath. When i scroll the collectionView the images are getting changed randomly? Please Help me. thanks in advance.
- (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"]];
}
推荐答案
问题在于 dequeueReusableCellWithReuseIdentifier
.
当您滚动 UICollectionview
时,单元格被重用,这是问题在 scrollview
中添加 Collectionview
.
When you scroll UICollectionview
then cell are reused that is problem
add Collectionview
inside scrollview
.
在里面试试这个:
Scroll_View 是您的滚动视图
Scroll_View is Your Scroll View
收藏就是你的收藏视图
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
self.Scroll_View.contentSize = CGSizeMake(self.view.frame.size.width, collectionView.contentSize.height);
CGRect fram_For_Collection_View = self.collection_view.frame;
fram_For_Collection_View.size.height = collectionView.contentSize.height;
self.collection.view.frame = fram_For_Collection_View;
}
这篇关于UICollectionView - 图像随机设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!