首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >集合单元格,它填充整个屏幕,一次只滚动一个?

集合单元格,它填充整个屏幕,一次只滚动一个?
EN

Stack Overflow用户
提问于 2015-10-26 01:21:46
回答 1查看 1K关注 0票数 0

我有一个UICollectionView,有20个细胞和一个部分。我制作了每个单元320宽304高。

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

这在iPhone 4s和iPhone 5/5s中工作得很好。使用iPhone 6/6 Plus时会出现问题。

当我scrollToItemAtIndexPath时,它一次滚动两个单元格。我怎样才能防止这种情况发生呢?我试图使单元格符合屏幕的宽度,但只出现了一个单元格,其余的UICollectionView都是黑色的。

编辑:

以下是数据源代码:

代码语言:javascript
运行
复制
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
    }
}

下面是我滚动它们的方式:

代码语言:javascript
运行
复制
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
      }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-07 13:32:09

我已经使用集合视图中的自定义FlowLayout解决了这个问题。下面是:

代码语言:javascript
运行
复制
    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)
    }
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33337100

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档