简体   繁体   English

如何在swift4中水平使用collectionView单元格

[英]how to use collectionView Cell horizontally in swift4

[![enter image description here][1]][1][![enter image description here][2]][2]I am using the collectionView cells horizontally to getting JSON data but the problem is I'm using the cell size height 100 and the width is view of the screen with paging enabled, but the problem is I can scroll it but I want to get edges 20 spacing of leading and trailing. [![在此处输入图像描述] [1]] [1] [![在此处输入图像描述] [2]] [2]我正在水平使用collectionView单元获取JSON数据,但是问题是我正在使用单元大小的高度为100,宽度为启用分页的屏幕view ,但问题是我可以滚动它,但我想使边缘的前导和尾部间距为20。 I'm moving the cells cant get the cell edges of leading and trailing like the first cell 我正在移动单元格,无法像第一个单元格一样获得前后的单元格边缘

class MainContainer: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

@IBOutlet weak var collectionview: UICollectionView!
@IBOutlet weak var FlowLayout: UICollectionViewFlowLayout!

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

    return CGSize(width: (self.view.frame.width - 20), height: 100)
}

You need to remove the minimum spacing in collectionView for cell as well as line 您需要删除collectionView中单元格和行的最小间距

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}

请尝试在控制器中的viewDidLoad()中进行更改,因为布局会以编程方式更改,因此它将覆盖滚动方向的默认值

FlowLayout.scrolldirection = .horizontal

Try below code: 试试下面的代码:

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

    return CGSize(width: (collectionView.frame.width - 20), height: 100)
}

**Step - 1 : ** Create CollectionView in StoryBoard (height,width,min-spacing,left-right) **步骤-1:**在StoryBoard中创建CollectionView(高度,宽度,最小间距,左右)

**Step - 2 : ** import (UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout) **步骤-2:**导入(UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout)

**Step - 3 : ** **步骤-3:**

@IBOutlet var collViewFeaturedEvents: UICollectionView!
@IBOutlet var collViewEventsHeight: NSLayoutConstraint!

**Step - 4 : ** Implement Methods **步骤-4:**实施方法

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
        if collectionView == self.collViewEvents
        {
            return self.ArrEvent.count
        }
    }


     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
        {

           {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollCell", for: indexPath) as! 让单元格= collectionView.dequeueReusableCell(withReuseIdentifier:“ CollCell”,for:indexPath)为! CollCell // let Listingimage = self.ArrListing[indexPath.item]["url"].stringValue return cell cell.layer.masksToBounds = false cell.layer.shadowOffset = CGSize(width: 2.5, height: 2.5) cell.layer.shadowRadius = 5 cell.layer.shadowOpacity = 0.5 cell.layer.cornerRadius = 6.0 cell.contentView.backgroundColor = UIColor.white CollCell //让Listingimage = self.ArrListing [indexPath.item] [“ url”]。stringValue返回单元格cell.layer.masksToBounds = false cell.layer.shadowOffset = CGSize(宽度:2.5,高度:2.5)cell.layer。 shadowRadius = 5 cell.layer.shadowOpacity = 0.5 cell.layer.cornerRadius = 6.0 cell.contentView.backgroundColor = UIColor.white

            let Listingimage = self.ArrListing[indexPath.item]["url"].stringValue
            print(Listingimage)
            let ImageUrl = URL_IMAGES.listing_logo+Listingimage

            cell.lblTitle?.text = self.ArrListing[indexPath.item]["business_name"].stringValue
            cell.lblDesc?.text = self.ArrListing[indexPath.item]["slogan"].stringValue
            cell.Imagview?.sd_setImage(with: URL(string: ImageUrl), placeholderImage: UIImage(named: "splash screen-test-compress.jpg"), options: [.continueInBackground,.lowPriority], completed: {(image,error,cacheType,url) in

                if error == nil
                {
                    cell.Imagview?.image = image
                }
                else
                {
                    cell.Imagview?.image =  UIImage(named: "splash screen-test-compress.jpg")
                }
            })
        }
}

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


func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
    {
let strId = self.ArrListing[indexPath.item]["id"].stringValue
            let detailVC = self.storyboard?.instantiateViewController(withIdentifier: "ListingDetails_InnerVC") as! ListingDetails_InnerVC
            detailVC.strID = strId
            self.navigationController?.pushViewController(detailVC, animated: true)
    }

self.collViewFeaturedEventsHeight.constant = self.collViewFeaturedEvents.collectionViewLayout.collectionViewContentSize.height self.view.updateConstraintsIfNeeded() self.collViewFeaturedEventsHeight.constant = self.collViewFeaturedEvents.collectionViewLayout.collectionViewContentSize.height self.view.updateConstraintsIfNeeded()

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

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