代码之家  ›  专栏  ›  技术社区  ›  Gorib Developer

如何在所有设备中使用swift4在CollectionView中显示两列

  •  14
  • Gorib Developer  · 技术社区  · 7 年前

    enter image description here

    为了在collectionView中只显示两列,我使用了这段代码

        let layout = self.collectionView.collectionViewLayout as! UICollectionViewFlowLayout
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0)
        layout.minimumInteritemSpacing = 4
        layout.itemSize = CGSize(width:(self.collectionView.frame.size.width - 10)/2,height: (self.collectionView.frame.size.height)/3)
    

    但它只在5s上正常显示,而不是在每一部iphone上。请帮忙。

    我想像这样展示我的视图控制器。请帮助任何人

    enter image description here

    5 回复  |  直到 7 年前
        1
  •  47
  •   Krunal    7 年前

    你一定失踪了 UICollectionViewDelegateFlowLayout

    试试看:

    // Source code
    import UIKit
    
    class CollectionVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
        @IBOutlet var collection: UICollectionView!
        override func viewDidLoad() {
            super.viewDidLoad()
            collection.delegate = self
            collection.dataSource = self
        }
    
        override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            //collection.reloadData()
        }
    
        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
            let flowayout = collectionViewLayout as? UICollectionViewFlowLayout
            let space: CGFloat = (flowayout?.minimumInteritemSpacing ?? 0.0) + (flowayout?.sectionInset.left ?? 0.0) + (flowayout?.sectionInset.right ?? 0.0)
            let size:CGFloat = (collection.frame.size.width - space) / 2.0
            return CGSize(width: size, height: size)
        }
    
    
        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return 50
        }
    
        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "testcell", for: indexPath)
            cell.backgroundColor = UIColor.red
            return cell
        }
    
    }
    

    //接口设计

    enter image description here

    结果:

    enter image description here

        2
  •  14
  •   aturan23 DairySeeker    4 年前

    确保您的协议符合您的 视图控制器 ,则,

    class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
    

    和设置 流程布局图 到您的 集合视图 对象

    在里面 viewDidLoad视图

    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .vertical //.horizontal
    layout.minimumLineSpacing = 5
    layout.minimumInteritemSpacing = 5
    collectionView.setCollectionViewLayout(layout, animated: true)
    

    并实施以下方法

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 1.0, left: 1.0, bottom: 1.0, right: 1.0)//here your custom value for spacing
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let lay = collectionViewLayout as! UICollectionViewFlowLayout
        let widthPerItem = collectionView.frame.width / 2 - lay.minimumInteritemSpacing
        
        return CGSize(width:widthPerItem, height:100)
    }
    
        3
  •  9
  •   tailor    7 年前

    将此代码添加到 viewDidLoad视图 。 根据您的需要更改高度。

    试试这个!

    if let layout = collectionView?.collectionViewLayout as? UICollectionViewFlowLayout{
            layout.minimumLineSpacing = 10
            layout.minimumInteritemSpacing = 10
            layout.sectionInset = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
            let size = CGSize(width:(collectionView!.bounds.width-30)/2, height: 250)
            layout.itemSize = size
    }
    
        4
  •  0
  •   Sagar Chauhan    7 年前

    我实现了以下代码,以在集合视图中显示两列。

    添加 UICollectionViewDelegateFlowLayout s以下方法:

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

    其中30表示单元格之间的左、右和中间空间。

    i、 e单元应在左侧添加10个像素,在右侧添加10个像素,在两个单元之间添加10个像素。

    选择您 collectionview 从序列图像板和大小检查器设置以下最小间距。

    我希望这能解决你的问题。

    如果有任何疑问,请告诉我。

    enter image description here

        5
  •  0
  •   Sergei Sevriugin    4 年前

    Swift 5和XCode 12.4的更新

        import UIKit
    
    class TwoColumnViewController: UIViewController {
    
        enum Section {
            case main
        }
    
        var dataSource: UICollectionViewDiffableDataSource<Section, Int>! = nil
        var collectionView: UICollectionView! = nil
    
        override func viewDidLoad() {
            super.viewDidLoad()
            navigationItem.title = "Two-Column Grid"
            configureHierarchy()
            configureDataSource()
        }
    }
    
    extension TwoColumnViewController {
        /// - Tag: TwoColumn
        func createLayout() -> UICollectionViewLayout {
            let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
                                                 heightDimension: .fractionalHeight(1.0))
            let item = NSCollectionLayoutItem(layoutSize: itemSize)
    
            let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
                                                  heightDimension: .absolute(44))
            let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitem: item, count: 2)
            let spacing = CGFloat(10)
            group.interItemSpacing = .fixed(spacing)
    
            let section = NSCollectionLayoutSection(group: group)
            section.interGroupSpacing = spacing
            section.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 10, bottom: 0, trailing: 10)
    
            let layout = UICollectionViewCompositionalLayout(section: section)
            return layout
        }
    }
    
    extension TwoColumnViewController {
        func configureHierarchy() {
            collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: createLayout())
            collectionView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
            collectionView.backgroundColor = .systemBackground
            view.addSubview(collectionView)
        }
        func configureDataSource() {
            let cellRegistration = UICollectionView.CellRegistration<TextCell, Int> { (cell, indexPath, identifier) in
                // Populate the cell with our item description.
                cell.label.text = "\(identifier)"
                cell.contentView.backgroundColor = .cornflowerBlue
                cell.layer.borderColor = UIColor.black.cgColor
                cell.layer.borderWidth = 1
                cell.label.textAlignment = .center
                cell.label.font = UIFont.preferredFont(forTextStyle: .title1)
            }
            
            dataSource = UICollectionViewDiffableDataSource<Section, Int>(collectionView: collectionView) {
                (collectionView: UICollectionView, indexPath: IndexPath, identifier: Int) -> UICollectionViewCell? in
                // Return the cell.
                return collectionView.dequeueConfiguredReusableCell(using: cellRegistration, for: indexPath, item: identifier)
            }
    
            // initial data
            var snapshot = NSDiffableDataSourceSnapshot<Section, Int>()
            snapshot.appendSections([.main])
            snapshot.appendItems(Array(0..<94))
            dataSource.apply(snapshot, animatingDifferences: false)
        }
    }