2013-12-18 43 views
1

我试图选择UICollectionView中的单元格,它会被选中,但向下滚动时会选择底部的其他单元格并向上滚动以显示其他要选择的其他单元格。收藏查看更改滚动选定的单元格

下面是我使用didSelectItemAtIndexPath

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

    cell = (CustomCollectionCell *)[collectionView cellForItemAtIndexPath:newIndex]; 

    NSString *strId = [[masterArray valueForKey:@"id"]objectAtIndex:indexPath.row]; 


    NSString *tempIndexRow = [NSString stringWithFormat:@"%ld",(long)indexPath.row]; 

    NSLog(@"%@, %@,%d ,%@, %d", strId,tempIndexRow,cell.imageView.tag,[boolArray objectAtIndex:indexPath.row],indexPath.row); 

    if (strId && [[boolArray objectAtIndex:indexPath.row] isEqualToString:@"False"] && cell.imageView.tag == indexPath.row) { 

     cell.selectedImage.image = [UIImage imageNamed:@"select.png"]; 

     [boolArray replaceObjectAtIndex:indexPath.row withObject:@"True"]; 
    } 
    else{ 
     cell.selectedImage.image = Nil; 

     [boolArray replaceObjectAtIndex:indexPath.row withObject:@"False"]; 
    } 
} 

代码这是我选择的第一次

enter image description here

这是我所得到的,当我向下滚动

enter image description here

感谢

+0

一样一样的tableview需要保存在某个地方哪个小区被选择,在这种方法“ - (UICollectionViewCell *)的CollectionView:(UICollectionView *)的CollectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath“管理你的图片 – Leena

回答

7

您需要设置所选项目的索引存储在一个阵列,并在cellForRowIndex时检查该数组的索引与indexPath.row像波纹管: -

selectedCellsArray ALLOC这个数组在ViewDIdLoad方法

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    cell = (CustomCollectionCell *)[collectionView cellForItemAtIndexPath:newIndex]; 
    if ([selectedCellsArray containsObject:[NSString stringWithFormat:@"%d",indexPath.row]] ) 
      { 
       [selectedCellsArray removeObject:[NSString stringWithFormat:@"%d",indexPath.row]]; 
       cell.selectedImage.image = Nil; 


      } 
      else 
      { 
       [selectedCellsArray addObject:[NSString stringWithFormat:@"%d",indexPath.row]]; 
       cell.selectedImage.image = [UIImage imageNamed:@"select.png"]; 
      } 
} 

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
     NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row]; 
     if ([selectedCellsArray containsObject:[NSString stringWithFormat:@"%@",rowNsNum]] ) 
     { 
      cell.selectedImage.image = [UIImage imageNamed:@"select.png"]; 


     } 
     else 
     { 
      cell.selectedImage.image = Nil; 
     } 
} 
+0

谢谢你!你让我今天一整天都感觉很好 :) –

0

它不选择另一个单元格。问题很可能是由于您重复使用单元格,并且在屏幕上返回时未正​​确重新设置它们。当单元格不可见时,它会卸载以节省内存。

所以最好先检查一下这个condtion以及在cellForItemAtIndexPath数据源

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

    { 

     //You cell initialization code. 

     if ([[boolArray objectAtIndex:indexPath.row] isEqualToString:@"True"]) { 

      cell.selectedImage.image = [UIImage imageNamed:@"select.png"]; 
     } 
     else{ 
      cell.selectedImage.image = Nil; 
     } 
    } 
相关问题