收集单元格可以填充整个屏幕宽度,并且一次只能在swift中滚动一个?

时间:2015-10-26 01:21:46

标签: ios swift uicollectionview uicollectionviewcell uicollectionviewlayout

我有UICollectionView,有20个单元格和1个部分。我将每个单元格的宽度设为320,将高度设为304。

我使用scrollToItemAtIndexPath(currentIndex + 1)使用集合视图底部的两个按钮以编程方式滚动集合视图。我只是逐个滚动它们。

这适用于iPhone 4s和iPhone 5 / 5s。使用iPhone 6/6 Plus时会出现此问题。

当我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
      }
}

1 个答案:

答案 0 :(得分: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)
    }
  }