title: UICollectionview cell selection
body: <p>i have made a grid of images and in order to show its selection i drew border for the image when selected. but the problem is when i select some image at the top and scroll down the grid of images, some other image at the bottom also seemed to be selected. below is my code snippet:</p>
  
  <pre><code>UINib *cellNib = [UINib nibWithNibName:@"collectionCell" bundle:nil];
  [self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"cellCV"];
  
  UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
  [flowLayout setItemSize:CGSizeMake(95, 95)];
  [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
  
  [self.collectionView setCollectionViewLayout:flowLayout];
  </code></pre>
  
  <p>Above has been added in viewDidLoad with the collection view cell designed in nib.</p>
  
  <p>and implemented following delegates:</p>
  
  <pre><code>-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  {   
  selectedImageIndex = indexPath.row;
  [collectionView reloadData]; 
  }
  
  -(CollectionCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  {
  UIImage *img = [imageArray objectAtIndex:indexPath.row];
  
  static NSString *cellIdentifier = @"cellCV";
  CollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:   
  cellIdentifier forIndexPath:indexPath];
  cell.imageView.image = img;
  cell.imageView.tag = indexPath.row;
  UIImageView *imgView = (UIImageView *)[cell viewWithTag:indexPath.row];
  if (indexPath.row == selectedImageIndex) {
      imgView.layer.borderWidth =  4.0;
      imgView.layer.borderColor = [UIColor redColor].CGColor;
      NSLog(@"selected indexpath: %d", indexPath.row);
  }
  else {
      imgView.layer.borderWidth =  0.0;
      imgView.layer.borderColor = nil;
  }
  return cell;    
  }
  </code></pre>
  
  <p>i could guess that something is going wrong with reusing the cell, but not sure and couldnt het an idea to resolve it.
  Waiting for any kind of help and suggestions.</p>
  
  <p>Thanks in advance.</p>