2013-05-21 27 views
1

嘗試在UICollectionView中選擇多個單元格,並在滾動時選擇變化。爲什麼發生,請指導。 以下是代碼。我試過了。滾動時選擇多個值隨機播放:UICollectionView

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
printf("Selected View index=%d",indexPath.row); 

itemPaths = [self.collectionView indexPathsForSelectedItems]; 

UICollectionViewCell* cell=[self.collectionView cellForItemAtIndexPath:indexPath]; 
cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"blue_s.png"]]; 
} 

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
UICollectionViewCell* cell=[self.collectionView cellForItemAtIndexPath:indexPath]; 
cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"yellow_seat.png"]]; 

} 

我還應該嘗試什麼。

+0

我在StackOverflow上找到了一個解決方法:在prepareForReuse中將單元格的選中狀態設置爲NO。爲此請參閱http://stackoverflow.com/questions/13556476/uicollectionview-with-multi-select-wont-select-more-than-a-dozen-of-items –

+0

@SAMIR RATHOD在哪裏聲明並調用此方法「準備重用「 –

回答

0

你可以做一兩件事來解決這個問題,你可以給標籤使用的UICollection查看自定義單元格的每個細胞,使其具有獨特的身份

0

因爲你很可能重用你的細胞,當你手上的細胞退出(cellForItemAtIndexPath)您需要檢查是否選擇了indexPath併爲您的單元格着色。您可能也想要使您的單元格變色,因爲在某些情況下,重用函數可能會在不應該的情況下發回彩色單元格。