简体   繁体   English

在 swift 中收集视图单元格滚动到中心时出现问题

[英]Issue while collection view cell scroll to center in swift

I have a collection view in my VC, I scroll collection view horizontally, I want to center each cell on scroll horizontally, I have tried a code but when it scrolls it shows next cell more left then the second one.This is my code,我在我的 VC 中有一个集合视图,我水平滚动集合视图,我想将每个单元格水平滚动居中,我尝试了一个代码,但是当它滚动时它显示下一个单元格比第二个更左。这是我的代码,

fileprivate let sectionInsets = UIEdgeInsets(top: 0, left: 15, bottom: 0, right: 15)

gridView.delegate = self
    gridView.dataSource = self
    gridView.register(ItemCollectionViewCell.self, forCellWithReuseIdentifier: "ItemCollectionViewCell")
    gridView.register(UINib(nibName: "ItemCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "ItemCollectionViewCell")
    gridView.showsHorizontalScrollIndicator = true
    gridView.bounces = true
    gridView.showsHorizontalScrollIndicator = false
    gridView.showsVerticalScrollIndicator = false

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
    return auctions?.properties?.count ?? 0
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
    let cell:ItemCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "ItemCollectionViewCell", for: indexPath) as! ItemCollectionViewCell;
    cell.propertyData = (auctions?.properties![indexPath.row])!
    if auctions?.status == AuctionStatus.Live.rawValue {
        cell.noOfBidsPlaced.isHidden = false
    }
    else {
        cell.noOfBidsPlaced.isHidden = true
    }
    cell.populateCell()

    return cell
}


func collectionView(_ collectionView: UICollectionView,
                    layout collectionViewLayout: UICollectionViewLayout,
                    sizeForItemAt indexPath: IndexPath) -> CGSize {
    let size = CGSize(width: 335 , height: 337)
    return size
}

//3
func collectionView(_ collectionView: UICollectionView,
                    layout collectionViewLayout: UICollectionViewLayout,
                    insetForSectionAt section: Int) -> UIEdgeInsets {
    return sectionInsets
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat
{
    return sectionInsets.left
}

func collectionView(_ collectionView: UICollectionView,
                    layout collectionViewLayout: UICollectionViewLayout,
                    minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return sectionInsets.left

}

How I can scroll each cell to center perfectly?如何滚动每个单元格以完美居中? Here is how it shows my collection on start,这是它在开始时显示我的收藏的方式, 在此处输入图像描述

Thats what it shows on scroll, it get cut from left side这就是它在滚动上显示的内容,它是从左侧剪切的在此处输入图像描述

Don't use static height and width.不要使用 static 高度和宽度。 call this function.Maybe its helpful for you:-称之为 function。也许它对你有帮助:-

// func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { // return CGSize(width: yourCollectionView.frame.size.width, height: yourCollectionView.frame.size.height ) // } // func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { // return CGSize(width: yourCollectionView.frame.size.width, height: yourCollectionView.frame.size.height ) / / }

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

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