繁体   English   中英

关于分页Swift的UICollectionView大小调整问题

[英]UICollectionView sizing issue on Paging Swift

在启用节和分页的情况下面临UICollectionview大小调整问题。 目前,我想以固定的单元格大小显示每节7列和6行,因此我如下创建。

        var margin: CGFloat = 10
        let cellsPerRow = 7
        let numberOfRows = 6;
        let cellHeight = 50
        let size = UIScreen.main.bounds.size.width - CGFloat(cellsPerRow*cellHeight)
        margin = size/CGFloat(cellsPerRow);

        collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: self.frame.size.width, height: 300), collectionViewLayout: UICollectionViewFlowLayout.init())


        guard let flowLayout = collectionView?.collectionViewLayout as? UICollectionViewFlowLayout else { return }
        flowLayout.minimumInteritemSpacing = margin
        flowLayout.minimumLineSpacing = 0
        flowLayout.sectionInset = UIEdgeInsets(top: margin, left: margin, bottom: margin, right: margin)
        flowLayout.scrollDirection = UICollectionViewScrollDirection.vertical

        collectionView.register(MyCollectionViewCell.self, forCellWithReuseIdentifier: "collectionCell")
        collectionView.isPagingEnabled = true;
        collectionView.dataSource = self;
        collectionView.delegate = self;


func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 20;
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 42;
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    //return collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath)
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier:"collectionCell", for: indexPath) as! MyCollectionViewCell
    cell.backgroundColor = UIColor.random()
    return cell;
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    let flowLayout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
    let marginsAndInsets = flowLayout.sectionInset.left + flowLayout.sectionInset.right + flowLayout.minimumInteritemSpacing * CGFloat(cellsPerRow - 1)
    let itemWidth = ((collectionView.bounds.size.width - marginsAndInsets) / CGFloat(cellsPerRow)).rounded(.down)
    return CGSize(width: itemWidth, height: itemWidth)

}

我只想在屏幕上显示收藏夹视图的一部分。 最初,它工作正常,但经过一些滚动(第6-7部分之后),我遇到了尺寸问题。 它显示了上一部分的一部分。 请参阅随附的问题截图。 在此处输入图片说明

请帮我。 先感谢您。

对于复杂的分页,最好使用scrollView Delegate: scrollViewWillEndDragging(_:withVelocity:targetContentOffset:) (请参阅文档

在您的代码中,它看起来像:

    func scrollViewWillEndDragging(_ scrollView: UIScrollView, 
                                   withVelocity velocity: CGPoint, 
                            targetContentOffset: UnsafeMutablePointer<CGPoint>) {

        let pageHeight : CGFloat = /* number of rows */ 6 * /* height of row */ 50 + /*  number of interspacing */ 5 * /* interspacing */ 10

        //This is where the scrollview Gonna stops
        let naturalDestination = targetContentOffset.pointee.y

        //Round it to the nearest page
        let computedDestination = round(targetContentOffset.pointee.y / pageHeight) * pageHeight

        //Edit the targetOffset
        targetContentOffset.pointee.y =  computedDestination

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM