title: indexPathForItemAtPoint Occasionally Returning nil
body: <p>In my app I have a <code>UICollectionView</code> which acts as a scrolling color-picker in my app.  I'm having a very rare bug where once in a blue moon the color will be set to the first value in the scroller upon button press, rather than the one associated with the button the user actually touched.  </p>
  
  <p>I isolated this to <code>indexPathForItemAtPoint</code> returning <code>nil</code> for the button press.  </p>
  
  <p>For now I've hacked around the problem by simply returning when the index path returned is <code>nil</code>, however, I'm concerned this behavior is still not correct for a couple reasons; </p>
  
  <ol>
  <li>First of all, I looked at the touch position in the debugger and it is valid within the bounds of the collection view.</li>
  <li>Second, the trigger to get the color associated with the button occurs when the event <code>UIControlEventTouchUpInside</code> happens for one of the buttons in the collection.  It therefore doesn't make sense to me that the code would be called for a touch not inside a button.  In this case I've swapped my current bug of getting the wrong color for a bug where a button will rarely not be responsive.  </li>
  </ol>
  
  <p>I did a bit of searching and unless I missed it, all I've seen on this site are cases where the code had an implementation error and was <em>always</em> receiving nil, but nothing quite like this.  </p>
  
  <p>I was hoping someone might be able to help me identify a cause so I can fix it or at least understand why the issue is occurring. </p>
  
  <p>The relevant functions are included below: </p>
  
  <pre><code>- (IBAction)myClickEvent:(id)sender event:(id)event
  {
      NSSet *touches = [event allTouches];
  
      UITouch *touch = [touches anyObject];
  
      CGPoint currentTouchPosition = [touch locationInView: self.collectionViewColors];
  
      NSIndexPath *indexPath = [self.collectionViewColors indexPathForItemAtPoint: currentTouchPosition];
  
      if(!indexPath)return;
  
      appState.currentColor = appState.colors[indexPath.row];
  
      const CGFloat *components = CGColorGetComponents(appState.currentColor.CGColor);
  
      self.redSlider.value = components[0];
      self.greenSlider.value = components[1];
      self.blueSlider.value = components[2];
  
      appState.setBackgroundImagePreview(nil);
  }
  </code></pre>
  
  <p>And the initialization function which sets the button event: </p>
  
  <pre><code>- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  {
      static NSString *identifier = @"Cell";
  
      UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
  
      UIButton *myButton = (UIButton *)[cell viewWithTag:100];
  
      [myButton  addTarget:self action:@selector(myClickEvent:event:) forControlEvents:UIControlEventTouchUpInside];
  
      /// abridged 
  
      [myButton setBackgroundImage:colorSquare forState:UIControlStateNormal];
  
      return cell;
  }
  </code></pre>
  
  <p>Thanks!</p>
  
  <p>Edit: I tried the following to get the touches, thinking that Apple might be sending multitouch / touches outside the button for some reason.  No dice.  It still returns a <code>nil</code> index path on occasion.  </p>
  
  <pre><code> NSSet *touches = [event touchesForView:sender];
  </code></pre>