2014-10-07 56 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

相关问题