Chirag Gajjar Chirag Gajjar - 12 days ago 9
iOS Question

how to single select cell in collection view..?

That are code properly working but when i will scroll my collection view then another cell also selected, for example 18 images are available and first show six at run time when i will select any one in six then also selected another 2 cell i confuse here why another two cell already selected please give me solution

enter image description here

here i have take 6 cell at main show storyboard

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);


This is my Didselect And didDeselect Method

-(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;

}

Answer

This is happening because the collectionView reuse the cells;

you should store the selected cell's IndexPath in a variable:

@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
}

than in "cell for row at indexPath" check :

- (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
}