0

我有一個UICollectionView有20個單元格和1節。我製作了每個單元320的寬度和高度爲304。收集單元格填充整個屏幕寬度,並且一次只能快速滾動一個單元格?

我使用scrollToItemAtIndexPath(currentIndex + 1)使用集合視圖底部的兩個按鈕以編程方式滾動集合視圖。我只會將它們1加1。

這適用於iPhone 4s和iPhone 5/5s。使用iPhone 6/6 Plus時出現問題。

當我I scrollToItemAtIndexPath它一次滾動2個單元格。 我該如何防止這種情況發生?我試圖讓電池適合屏幕的寬度,但只有一個電池出現,其餘的UICollectionView是黑色的。

編輯:

下面是數據源代碼:

extension ViewController: UICollectionViewDataSource { 

    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 
    return 1 
    } 

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return 32 
    } 

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("collectionCell", forIndexPath: indexPath) as! CollectionCell 
    cell.backgroundColor = UIColor.whiteColor() 

    self.current = indexPath 

    self.configureCell(cell, atIndexPath: indexPath) as! CollectionCell 
    return cell 
    } 

    func configureCell(cell: CollectionCell, atIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let string = textArray[indexPath.item % 20] as String 
    cell.textView.text = string 
    cell.textView.backgroundColor = UIColor.cloudsColor() 
    return cell 
    } 
} 

這裏是我怎麼滾動他們:

func buttonSelected(sender: UIButton) { 
    switch sender.tag { 
     case 0: 
     let previousItem: NSIndexPath = NSIndexPath(forItem: self.current.item - 1, inSection: self.current.section) 
     self.collectionView?.scrollToItemAtIndexPath(previousItem, atScrollPosition:UICollectionViewScrollPosition.CenteredHorizontally, animated:true) 
     case 1: 
     let nextItem: NSIndexPath = NSIndexPath(forItem: self.current.item + 1, inSection: self.current.section) 
     self.collectionView?.scrollToItemAtIndexPath(nextItem, atScrollPosition:UICollectionViewScrollPosition.CenteredHorizontally, animated:true) 
     default: break 
     } 
} 
+0

你可以請張貼一些代碼和截圖嗎? – anhtu

+0

我發佈了我使用的代碼。 – eeschimosu

回答

0

我已經解決了這個使用一個自定義的FlowLayout在收集視圖。 它在這裏:

class CenterFlowLayout: UICollectionViewFlowLayout { 

    override func targetContentOffsetForProposedContentOffset(proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint { 
     if let cv = self.collectionView { 
     let cvBounds = cv.bounds 
     let halfWidth = cvBounds.size.width * 0.5 
     let proposedContentOffsetCenterX = proposedContentOffset.x + halfWidth 
     if let attributesForVisibleCells = self.layoutAttributesForElementsInRect(cvBounds) as [UICollectionViewLayoutAttributes]! { 
     var candidateAttributes: UICollectionViewLayoutAttributes? 
     for attributes in attributesForVisibleCells { 
     // == Skip comparison with non-cell items (headers and footers) == // 
      if attributes.representedElementCategory != UICollectionElementCategory.Cell { 
    continue 
      } 
      if let candAttrs = candidateAttributes { 
       let a = attributes.center.x - proposedContentOffsetCenterX 
       let b = candAttrs.center.x - proposedContentOffsetCenterX 
      if fabsf(Float(a)) < fabsf(Float(b)) { 
       candidateAttributes = attributes 
      } 
     } else { // == First time in the loop == // 
     candidateAttributes = attributes 
     continue 
     } 
    } 
    return CGPoint(x : candidateAttributes!.center.x - halfWidth, y : proposedContentOffset.y) 
    } 
} 
    // Fallback 
    return super.targetContentOffsetForProposedContentOffset(proposedContentOffset) 
    } 
    } 
相關問題