UICollectionView单元格为什么居中?

时间:2018-12-21 08:54:13

标签: ios objective-c

我的collectionView有问题。 其实我有这个算法:
-第一个单元格的宽度是screen_width / 2
-第二个单元格的宽度为:screen_width
-第三个单元格的宽度为screen_width / 2
-第四个单元格的宽度是:screen_width。

以此类推。

这是我在方法中的代码

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath:

    userStory.typePhotoStory = ( (indexPath.row + 1) % 3) == 0 ? @"landscape" : ((indexPath.row + 1) % 3) == 1 ? @"portrait": @"landscape";
    if ([userStory.typePhotoStory isEqualToString:@"portrait"]) {
        cellSizeFinalWidth = cellSize / 2 ;
        cellSizeFinalHeight = cellSizeFinalWidth + 20 ;
    } else {
        cellSizeFinalWidth = cellSize + padding ;
        cellSizeFinalHeight = cellSize / 2 + 20 ;
    }
}

但是我看到,第一个和第三个单元格位于屏幕的中央,而不是从左侧开始。 请帮助我解决这个问题?

1 个答案:

答案 0 :(得分:0)

您可以直接使用此代码,

  1. 创建一个新文件,并将其粘贴到下面的代码中以用于自定义布局。

    import UIKit
    
    protocol NewLayoutDelegate: class {
        func collectionView(_ collectionView:UICollectionView, SizeOfCellAtIndexPath indexPath:IndexPath) -> CGSize
    }
    
    
    class NewFlowLayout: UICollectionViewFlowLayout {
    
        weak var delegate : NewLayoutDelegate?
    
        fileprivate var numberOfColumns = 1
        fileprivate var cellPadding: CGFloat = 6
        fileprivate var cache = [UICollectionViewLayoutAttributes]()
    
    
        fileprivate var contentHeight: CGFloat = 0
    
        fileprivate var contentWidth: CGFloat {
            guard let collectionView = collectionView else {
                return 0
            }
            let insets = collectionView.contentInset
            return collectionView.bounds.width - (insets.left + insets.right)
        }
    
        override var collectionViewContentSize: CGSize {
            return CGSize(width: contentWidth, height: contentHeight)
        }
    
        override func prepare() {
            super.prepare()
    
            guard cache.isEmpty == true, let collectionView = collectionView else {
                return
            }
    
            let xOffset : CGFloat = 0
            let column = 0
            var yOffset = [CGFloat](repeating: 0, count: numberOfColumns)
    
            for item in 0 ..< collectionView.numberOfItems(inSection: 0) {
    
                let indexPath = IndexPath(item: item, section: 0)
                let aSize = delegate?.collectionView(collectionView, SizeOfCellAtIndexPath: indexPath)
                let height = cellPadding * 2 + (aSize?.height)!
                let frame = CGRect(x: xOffset, y: yOffset[column], width: (aSize?.width)!, height: height)
                let insetFrame = frame.insetBy(dx: cellPadding, dy: cellPadding)
    
                let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
                attributes.frame = insetFrame
                cache.append(attributes)
    
                contentHeight = max(contentHeight, frame.maxY)
                yOffset[column] = yOffset[column] + height
    
    
            }
        }
    
        override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
    
            var visibleLayoutAttributes = [UICollectionViewLayoutAttributes]()
    
            for attributes in cache {
                if attributes.frame.intersects(rect) {
                    visibleLayoutAttributes.append(attributes)
                }
            }
            return visibleLayoutAttributes
        }
    
        override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
            return cache[indexPath.item]
        }
    
    }
    
  2. 在您的控制器文件中

    import UIKit
    
    class NewCell: UICollectionViewCell {
    
    }
    
    class NewViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, NewLayoutDelegate {
    
        @IBOutlet weak var myCollectionView: UICollectionView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            myCollectionView.collectionViewLayout = NewFlowLayout()
    
            if let layout = myCollectionView.collectionViewLayout as? NewFlowLayout {
                layout.delegate = self
            }
    
        }
    
        // Do size logic here
        func collectionView(_ collectionView: UICollectionView, SizeOfCellAtIndexPath indexPath: IndexPath) -> CGSize {
    
            let insets = collectionView.contentInset
    
            if indexPath.item % 2 == 0 {
                return CGSize(width: (collectionView.frame.size.width - (insets.left + insets.right))/2,
                              height: 60)
            }
    
            return CGSize(width: collectionView.frame.size.width - (insets.left + insets.right),
                          height: 60)
        }
    
        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return 10
        }
    
        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "NewCell", for: indexPath) as! NewCell
    
            return cell
        }
    }
    
  3. 输出

enter image description here