2013-12-18 42 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; 
     } 
    } 
相關問題