2014-04-11 66 views
2

我在我的应用程序中使用了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"]]; 

} 
+4

请附上您的代码 – dKrawczyk

+0

请出示您的cellForItemAtIndexPath()方法 –

+0

没有看到你的代码很难告诉,但我的猜测是,单元格正在重新使用,当你滚动。 – jervine10

回答

0

您的-collectionView:didSelectItemAtPath:正在向单元添加新的图像视图。当单元格被重用时,没有任何东西正在移除该图像视图。所以,当你说:

UICollectionViewCell *cell = [collection dequeueReusableCellWithReuseIdentifier:uio 

forIndexPath:indexPath];

在你的-collectionView:cellForItemAtIndexPath:中,你可能会找回一些已经有一个或多个图像视图的单元格。

我的建议是将图像视图添加到单元格原型中的单元格,可能是在故事板或单元格的初始化程序中。让您的-collectionView:cellForItemAtIndexPath:将该图像视图的图像设置为给定路径的正确图像。

0

发生了什么是UICollectionView重用细胞。所以在didSelectItemAtIndexPath中:你设置了单元格的背景,但是然后UICollectionView根据需要重用了同一个单元格(并且你没有重置cellForItemAtIndexPath中的cell.backgroundView :)。

解决此问题的方法是维护所选单元格的NSIndexSet。在didSelectItemAtIndexPath中:您可以添加所选项目的索引,然后通过调用reloadItemsAtIndexPaths强制重新加载该项目。然后,在您的cellForItemAtIndexPath中:检查索引集以查看是否包含所选索引,如果是,请设置单元格的backgroundView。

2

这是因为您重复使用了您的手机。一个选项是让一个字典变量表示你的单元格已经被选中,并且如果它没有被重置过的话。

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"index %@",indexPath); 
    UICollectionViewCell *cell = [collection cellForItemAtIndexPath:indexPath]; 

    cell.backgroundView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"download.jpg"]]; 

    [selectedDictionary setObject:[NSNumber numberWithBool:YES] forKey:[NSNumber numberWithInteger:indexPath.row]]; 
} 

然后在您的cellForItemAtIndexPath方法,如果您使用某种类型的对象模型,你会检查值

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 

    UICollectionViewCell *cell = [collection dequeueReusableCellWithReuseIdentifier:uio 
forIndexPath:indexPath]; 
    BOOL selected = [[selectedDictionary objectForKey:[NSNumber numberWithInteger:indexPath.row]] boolValue]; 

    if(selected){ 
     cell.backgroundView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"download.jpg"]]; 
    }else{ 
     cell.backgroundView = nil; 
    } 

    cell.backgroundColor = [UIColor whiteColor]; 
    return cell; 
} 

当然,这将适当有选择的变量在这里,你赢了”不再需要nsdictionary。

0

几天前我也有同样的问题&我在这里发表了一个问题。这是我得到的答案&它适用于我。

Collection View Cell multiple item select Error

而且还如果你使用的是自定义单元格,你可以将此代码添加到该小区&的init方法,将工作过。

 CGFloat borderWidth = 6.0f; 
     UIView *bgView = [[UIView alloc] initWithFrame:frame]; 
     bgView.layer.borderColor = [UIColor redColor].CGColor; 
     bgView.layer.borderWidth = borderWidth; 
     self.selectedBackgroundView = bgView; 
2

问题是dequeueReusableCellWithReuseIdentifier

滚动时UICollectionview然后细胞被重用是问题 添加Collectionviewscrollview

试试这个里面:

Scroll_View是滚动视图

收集你的CollectionView

-(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; 
}