2016-04-26 95 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 
} 
相关问题