2014-10-07 85 views
-1
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ 
     UIImageView *img; 
     UICollectionViewCell *myCell=[collectionView cellForItemAtIndexPath:indexPath]; 
     img=(UIImageView*)[myCell viewWithTag:101]; 
     if (myCell.selected) { 
      [myCell.img setImage:[UIImage imageNamed:@"images-2.png"]]; 
     } 

     [collectionView reloadData]; 
    } 

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{ 
     UIImageView *img; 
     UICollectionViewCell *myCell=[collectionView cellForItemAtIndexPath:indexPath]; 
     img=(UIImageView*)[myCell viewWithTag:101]; 
     [img setImage:[UIImage imageNamed:@"images.png"]]; 
     [collectionView reloadData]; 
    } 

回答

0

問題與重載數據後[collectionView reloadData]; 你會改變圖像中cellForItemAtIndexPath

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
    { 
    UICollectionViewCell *cell = (UICollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath]; 
    if(indexPath.row == selectedIndexPath) 
    { 
    [img setImage:[UIImage imageNamed:@"selected image"]]; 
    } 
    else 
    { 
    [img setImage:[UIImage imageNamed:@"images.png"]]; 
    } 
} 

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    selectedIndexPath=indexPath.row; 
    [collectionView reloadData]; 
} 

注:最初設置selectedIndexPath = -1,則表明在集合視圖中的答覆

+0

非常感謝回覆!如何獲得selectcell? – 2014-10-08 06:12:42

+0

由於NANNAV是一個很好的幫助!我想你的ID聯繫..請 – 2014-10-09 08:20:55

+0

@ mubibegum有沒有在這個網站上的個人聊天,在這個網站添加您的問題,任何一個幫助給你。 – NANNAV 2014-10-09 08:40:49

0

請勿在委託回調中調用reloadData。這很容易導致未定義的行爲,並導致您提到的有關集合視圖的問題無法正確處理選擇狀態。

通常調用reloadData刪除所有選擇。

+0

由於沒有選擇我不是!重新加載單元格,但我想改變單元格的圖像..你可以建議如何做到這一點? – 2014-10-08 06:26:06

相關問題