3

UICollectionView中,我有一個自定義UICollectionViewCellClass,其中prepareForReuse被默認格式化工作人員覆蓋。重複使用單元格UICollectionView - 某些單元格未被選中

我有一個NSMutableArray包含NSIndexPathsdidSelectItemAtIndexPath:

cellForItemAtIndexPath:我重新格式化選定的單元格,使它們顯示爲選中狀態。

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

ButtonCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ButtonCell" forIndexPath:indexPath]; 

NSString *title = self.ingredientsBook.names[indexPath.item]; 

cell.label.text = title; 

if ([self isSelectedIndexPath:indexPath]){ 

    cell.backgroundColor = [UIColor whiteColor]; 
    cell.label.textColor = [UIColor blueColor]; 
} 
return cell; 

} 

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ 

self.searchButton.enabled = YES; 

ButtonCell *cell = (ButtonCell *)[collectionView cellForItemAtIndexPath:indexPath]; 
[selectedCellIndexPaths addObject:indexPath]; 
NSLog(@"%@", selectedCellIndexPaths); 
cell.backgroundColor = [UIColor whiteColor]; 
cell.label.textColor = [UIColor blueColor]; 

NSString *name = self.ingredientsBook.names[indexPath.item]; 
[self.selectedIngredientNames addObject:name]; 


} 

問題是,當我點擊第一個單元格時,不可能選擇第16或第17個。 或者如果我點擊前三個,不可能選擇最後三個。 didSelectItemAtIndexPath沒有被稱爲我想。

我覺得它必須是非常簡單的東西,但我現在看不到它。

我試圖把NSLogs放在shouldSelectItemAtIndexPath中,以瞭解該方法是否被調用並且該方法根本沒有被調用。發生這種情況時,所選一個與有問題的單元之間的距離爲16個單元。

這裏有其他數據源的方法和isSelectedIndexPath:

-(BOOL)isSelectedIndexPath:(NSIndexPath *)indexPath{ 

for (NSIndexPath *test in selectedCellIndexPaths){ 
    if (test == indexPath){ 
     return YES; 
    } 
} 

return NO; 
} 

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ 

return [self.ingredientsBook.names count]; 
} 

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ 

return 1; 
} 

-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath{ 

NSLog(@"%@", indexPath); 

return YES; 
} 


-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{ 

self.searchButton.enabled = ([[collectionView indexPathsForSelectedItems] count] > 0); 

ButtonCell *cell = (ButtonCell *)[collectionView cellForItemAtIndexPath:indexPath]; 
cell.backgroundColor = [UIColor blackColor]; 
cell.label.textColor = [UIColor whiteColor]; 

[selectedCellIndexPaths removeObject:indexPath]; 
NSString *name = self.ingredientsBook.names[indexPath.item]; 
[self.selectedIngredientNames removeObject:name]; 



} 
+0

什麼是isSelectedIndexPath?你在哪裏創建它?如果[self isSelectedIndexPath:indexPath]返回false,我認爲你還需要一個「else」部分來說明單元格應該是什麼樣子。 – rdelmar 2013-04-06 15:59:57

+0

它是一種檢查indexPath是否包含在NSIndexPaths的NSMutableArray中的方法。當indexPath等於數組中的某一個時,它總是返回false。 – ragnarok 2013-04-06 16:36:53

+0

好的,你是否在if語句中添加了「else」部分以查看是否有幫助。由於單元重用,如果if語句爲false,則需要將單元格的外觀設置爲未選中狀態。 – rdelmar 2013-04-06 16:45:25

回答

2

我發現了兩個問題。 prepareForReuse方法似乎是搞砸了,所以我剛刪除它。但是,主要的問題是你正在實現的方式isSelectedIndexPath :.只要您在循環查找項目時發現第一個選定項目,它就會返回YES並退出循環。你想要做什麼,只是檢查indexPath包含在selectedCellIndexPaths數組中:

-(BOOL)isSelectedIndexPath:(NSIndexPath *)indexPath{ 

    if ([selectedCellIndexPaths containsObject:indexPath]) { 
     return YES; 
    }else{ 
     return NO; 
    } 
} 

或者,如果你喜歡使用更簡潔的語法,你可以替換的if-else塊:

return ([selectedCellIndexPaths containsObject:indexPath])? YES : NO; 
+0

即使這些更改仍存在問題。但是非常感謝您的耐心和堅持。 – ragnarok 2013-04-06 21:41:21

+0

對不起,我想刪除prepareForReuse。現在一切都很精彩。再次感謝耐心和堅持。 – ragnarok 2013-04-06 21:49:10

+1

Just:'return [selectedCellIndexPaths containsObject:indexPath];' – k06a 2013-10-02 17:27:50

1

我最近面對與我的應用程序完全相同的問題。 UICollectionViewCell選擇在iOS 8.3之前正常工作,隨後我開始看到一些奇怪的行爲。實際上未選中的單元格會顯示爲已選中,其他單元格似乎無法選中。

我對一個子類UICollectionViewCell實現爲這樣的兩個定製setSelectedprepareForResuse方法:

-(void)setSelected:(BOOL)selected 
{ 
    [super setSelected:selected]; 

    if (selected) 
    { 
     [[self selectedIndicator] setHidden:NO]; 
    } 
    else 
    { 
     [[self selectedIndicator] setHidden:YES]; 
    } 
} 

-(void)prepareForReuse 
{ 
    [[self imageView] setImage:nil]; 
} 

prepareForReuse該方法簡單地復位在定製單元的圖像視圖。

在我的prepareForReuse方法中,我沒有撥打[super prepareForReuse](根據文檔默認情況下不做任何操作)。當我將呼叫添加到[super prepareForReuse]時,所有選擇都按預期工作。儘管Apple聲明默認實現什麼都不做,但他們也建議應該調用super。按照這個建議解決了我的問題。

0

在iOS 10中,我發現在啓用預取時,以編程方式清除啓用了多選的UICollectionView是錯誤的。當然,默認情況下預取在iOS 10中是打開的。