2016-04-26 79 views
2

這是代碼工作正常,但是當我將滾動我的收藏視圖,則另一小區也被選中,例如18個映像可用,首先顯示了六個在運行時,當我將選擇在六個任意一個,然後又選擇了其他2小區i爲什麼另外兩個小區已選定在這裏混淆了,請給我解決如何在集合視圖中選擇單元格?

enter image description here

在這裏,我拿6芯在故事情節主要表現

UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout*)self.collectionView.collectionViewLayout; 

flowLayout.minimumLineSpacing = 15; 
CGFloat availableWidthForCells = CGRectGetWidth(self.collectionView.frame) - flowLayout.sectionInset.left - flowLayout.sectionInset.right - flowLayout.minimumInteritemSpacing *2; 

cellWidth = availableWidthForCells /6; 
    NSLog(@"cellWidth:%f",cellWidth); 
flowLayout.itemSize = CGSizeMake(cellWidth, cellWidth); 

這是我Didselect而didDeselect方法

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

    UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath]; 

    cell.layer.cornerRadius = cellWidth/2.0; 
    cell.layer.backgroundColor = [UIColor blackColor].CGColor; 
    NSLog(@"INDEXPATH:-%ld",(long)indexPath.row); 
} 
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{ 

    UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath]; 
    cell.layer.cornerRadius = cellWidth/2.0; 
    cell.layer.backgroundColor = [UIColor whiteColor].CGColor; 

} 

回答

3

這是因爲collectionView重用單元;

你應該存儲選定單元格的IndexPath在一個變量:

@property (nonatomic, retain) NSIndexPath *selectedIndexPath; 

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

    UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath]; 

    cell.layer.cornerRadius = cellWidth/2.0; 
    cell.layer.backgroundColor = [UIColor blackColor].CGColor; 
    NSLog(@"INDEXPATH:-%ld",(long)indexPath.row); 

    self.selectedIndexPath = indexPath 
} 
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{ 

    UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath]; 
    cell.layer.cornerRadius = cellWidth/2.0; 
    cell.layer.backgroundColor = [UIColor whiteColor].CGColor; 

    self.selectedIndexPath = nil 
} 

比 「在indexPath細胞用於行」 檢查:

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

    UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath]; 

    if (self.selectedIndexPath != nil && indexPath == self.selectedIndexPath) { 
     cell.layer.cornerRadius = cellWidth/2.0; 
     cell.layer.backgroundColor = [UIColor blackColor].CGColor; 
    else { 
     cell.layer.cornerRadius = cellWidth/2.0; 
     cell.layer.backgroundColor = [UIColor whiteColor].CGColor; 
    } 

    return cell 
} 
+1

很多感謝您的代碼工作 –

0

這是因爲細胞被重用。當你

UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath]; 

和改變單元的角半徑,你實際上改變了多個單元的角半徑。所以你應該這樣做:

let cell = collectionView.dequeueReusableCellWithReuseIdentifier("YourCell", forIndexPath: indexPath) as! YourCell 
0

enter image description here

在這裏看看我將只選擇一個,然後第七圖像自動選擇 我有六幅圖像

1

感謝顯示屏阿爾貝託Scampini 此代碼爲SWIFT 3.1

var selectedIndexPath: IndexPath? 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

    let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: "SegmentChoiceCVCell", for: indexPath) as! SegmentChoiceCVCell 
    //configure cell 

    if selectedIndexPath != nil && indexPath == selectedIndexPath { 
     cell.checkIcon.backgroundColor = UIColor.black 

    }else{ 

     cell.checkIcon.backgroundColor = UIColor.white 
    } 

    return cell 

} 



func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 

    let cell = collectionView.cellForItem(at: indexPath) as! SegmentChoiceCVCell 

     cell.checkIcon.backgroundColor = UIColor.black 
    self.selectedIndexPath = indexPath 

} 

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) { 
    let cell: SegmentChoiceCVCell = collectionView.cellForItem(at: indexPath) as! SegmentChoiceCVCell 

     cell.checkIcon.backgroundColor = white 
    selectedIndexPath = nil 
} 
相關問題