2014-04-11 67 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; 
}