【问题标题】:How to make UICollectionView class programmatically in iOS Swift 3 without using storyboard?如何在不使用情节提要的情况下在 iOS Swift 3 中以编程方式制作 UICollectionView 类?
【发布时间】:2017-12-20 08:47:09
【问题描述】:

我创建了一个继承自 UICollectionView 的类 GalleryCollectionViewController,如下所示:

import UIKit  

class GalleryCollectionViewController: UICollectionViewController {

var dataSourceArr:Array<UIImage>!
    override convenience init(collectionViewLayout layout: UICollectionViewLayout) {
        self.init()
        collectionView?.collectionViewLayout = layout
        collectionView!.register(GalleryCollectionViewCell.self, forCellWithReuseIdentifier: "cell")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

   // MARK: UICollectionViewDataSource

    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        if dataSourceArr.count != 0 {
            return dataSourceArr.count
        }
        return 0
    }

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

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! GalleryCollectionViewCell

        cell.imageView.image = dataSourceArr[indexPath.row]

        return cell
    }  

GalleryCollectionViewCell 已定义。

在根控制器中设置这个 viewDidLoad :

let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
layout.itemSize = CGSize(width: 90, height: 120)
let galleryColVC = GalleryCollectionViewController(collectionViewLayout: layout)
galleryColVC.dataSourceArr = photoLibraryImagesArr
self.present(galleryColVC, animated: true, completion: nil)

但在 UICollectionView 中出现此错误:

*** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“UICollectionView 必须是 用非零布局参数初始化'

请帮忙解决这个问题。

【问题讨论】:

  • 这似乎解释了您得到崩溃的原因:stackoverflow.com/questions/24288927/… 您需要使用初始化程序进行实例化: init(frame: CGRect, collectionViewLayout: UICollectionViewLayout) 并且您只需在 GalleryCollectionViewController 中使用 self.init()类

标签: ios swift layout swift3 uicollectionview


【解决方案1】:

这是一个小例子

import UIKit

class ViewController: UIViewController {

  let cellId = "cellId"

  let newCollection: UICollectionView = {
     let layout = UICollectionViewFlowLayout()
     layout.scrollDirection = .horizontal
     let collection = UICollectionView(frame: CGRect(x: 0, y: 0, width: 0, height: 0), collectionViewLayout: layout)
     collection.translatesAutoresizingMaskIntoConstraints = false
     collection.backgroundColor = UIColor.darkGray
     collection.isScrollEnabled = true
    // collection.contentSize = CGSize(width: 2000 , height: 400)
     return collection
  }()

  override func viewDidLoad() {
      super.viewDidLoad()

      view.addSubview(newCollection)
      newCollection.delegate = self
      newCollection.dataSource = self
      newCollection.register(CustomeCell.self, forCellWithReuseIdentifier: cellId)
      setupCollection()
  }


  func setupCollection(){

      newCollection.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
      newCollection.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
      newCollection.heightAnchor.constraint(equalToConstant: 400).isActive = true
      newCollection.widthAnchor.constraint(equalToConstant: view.frame.width).isActive = true 
  }

}

extension ViewController: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       return 100
    }


   func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
       let cell = newCollection.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! CustomeCell
       cell.backgroundColor = .white
       cell.layer.cornerRadius = 5
       cell.layer.borderWidth = 2
       cell.layer.borderColor = UIColor.white.cgColor
       cell.layer.shadowOpacity = 3
       return cell
   }

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

       return CGSize(width: 150, height: 250)
   }

   func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
       return UIEdgeInsets(top: 3, left: 3, bottom: 3, right: 3)
   }
}

class CustomeCell: UICollectionViewCell {

   override init(frame: CGRect) {
       super.init(frame: frame)
       setupViews()
   }

   required init?(coder aDecoder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }
}

【讨论】:

    猜你喜欢
    • 2019-11-03
    • 2017-10-19
    • 1970-01-01
    • 2017-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-20
    相关资源
    最近更新 更多