title: How to jump to any Cell in a UICollectionView by using a custom layout?
body: <p>I have 40 cells in my horizontal UICollectionView and a button.</p>
  
  <p>I can jump from cell number 5 to cell number 10 when I click on the button.</p>
  
  <p>But as soon as I want to go to a further cell ( for example from 5 to 25 ), </p>
  
  <p>it doesn't work, and instead it goes to 0.</p>
  
  <p>code: </p>
  
  <pre><code>func setValue(value: Int, animated: Bool) {
     self.value = value
     if let row = find(values, value) {
     let indexPath = NSIndexPath(forRow: row, inSection: 0)
     selectedCell = collectionView.cellForItemAtIndexPath(indexPath) as? StepperCell
     collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .CenteredHorizontally, animated: animated)
    }
   }
  
  override func prepareLayout() {
     let start = Int(collectionView!.bounds.size.width * 0.5) - statics.width / 2
     let length = collectionView!.numberOfItemsInSection(0)
  
  if cache.isEmpty &amp;&amp; length &gt; 0 {
     for item in 0..&lt;length {
       let indexPath = NSIndexPath(forItem: item, inSection: 0)
       let attributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
       attributes.frame = CGRect(
        x: start + (statics.width + statics.padding) * indexPath.item,
        y: 0,
        width: statics.width,
        height: statics.height
      )
      cache.append(attributes)
    }
    contentWidth = CGRectGetMaxX(cache.last!.frame) + CGFloat(start)
     }
   }
  
   override func layoutAttributesForElementsInRect(rect: CGRect) -&gt; [AnyObject]? {
     var layoutAttributes = [UICollectionViewLayoutAttributes]()
     for attributes in cache {
        if CGRectIntersectsRect(attributes.frame, rect) {
          layoutAttributes.append(attributes)
      }
    }
    return layoutAttributes
    }
   }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/3cKaa.png" alt="enter image description here"></p>