ios - 以编程方式在带有商店的 ViewController 中添加两个 UICollectionView

标签 ios swift uicollectionview

试图从 Storyboard 中跳槽。我正在尝试将两个 UIViewController 放入一个 View 中,然后水平滚动。

所以首先,我进入应用委托(delegate)

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    window = UIWindow(frame: UIScreen.main.bounds);
    window?.makeKeyAndVisible()

    var homeViewController = ViewController()
    let shirtStore = ShirtStore()
    let pantStore = PantStore()
    homeViewController.shirtStore = shirtStore
    homeViewController.pantStore = pantStore

    window?.rootViewController = UINavigationController(rootViewController: ViewController())


    return true
}

我不确定我是否加载了第一个 homeViewController。

然后,在我的 ViewController 中我有:

import UIKit

class ViewController: UIViewController, 
UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {



let collectionViewShirts = UICollectionView()
let collectionViewPants = UICollectionView()
let collectionViewShirtsIdentifier = "CollectionViewShirtsCell"
let collectionViewPantsIdentifier = "CollectionViewPantsCell"

var shirtStore: ShirtStore!
var pantStore: PantStore!

override func viewDidLoad() {
    super.viewDidLoad()
    navigationItem.title = "Hanger"

    view.backgroundColor = UIColor.red

    collectionViewShirts.delegate = self
    collectionViewPants.delegate = self

    collectionViewShirts.dataSource = self
    collectionViewPants.dataSource = self

    self.view.addSubview(collectionViewShirts)
    self.view.addSubview(collectionViewPants)

    collectionViewShirts.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "CollectionViewShirtsCell")
}

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

if collectionView == self.collectionViewShirts {
        let cellA = collectionView.dequeueReusableCell(withReuseIdentifier: collectionViewShirtsIdentifier, for:  indexPath as IndexPath)

        // Set up cell
        cellA.backgroundColor = UIColor.blue
        return cellA
    }

    else {
        let cellB = collectionView.dequeueReusableCell(withReuseIdentifier: collectionViewPantsIdentifier, for: indexPath as IndexPath)

        // ...Set up cell
         cellB.backgroundColor = UIColor.red
        return cellB
    }
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
    if(collectionView == collectionViewShirts)
    {
        return shirtStore.allShirts.count
    }
    else if (collectionView == collectionViewPants)
    {

        return 5//pantStore.allPants.count
    }
    else
    {
        return 5//shoeStore.allShoes.count
    }
  }
}

我的应用程序因布局参数为零而终止。我错过了什么。构建时没有警告。

最佳答案

您不能在没有参数的情况下初始化 UICollectionView。您需要提供一个 collectionViewLayout 参数,以便 Collection View 知道如何安排其内容。

相反,创建一个布局对象并使用它来初始化 Collection View 。在 Swift 中执行此操作的一个好方法是使用惰性闭包来初始化属性。

public lazy var collectionViewShirts: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
    collectionView.delegate = self
    collectionView.dataSource = self
    // any other configuration here
    return collectionView
}()

关于ios - 以编程方式在带有商店的 ViewController 中添加两个 UICollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45850181/

相关文章:

objective-c - NSError:方法中传递错误

objective-c - 为什么我使用 MapViewController 将无法识别的选择器发送到实例异常? (iOS)

ios - 触发 IBAction 时更新用于创建 UITableView 的数组数据

swift - 这是 WWDC 2014 示例代码的错误吗?

c# - 启用环绕的线性旋转木马

ios - 父/子和一对一/多逆向关系之间的核心数据差异

ios - Unity3d Ios 应用程序的 Keen.io 实现

swift - 如何使用 JSON 序列化对象将其转换为 POST 调用

swift - 在 UICollectionView sizeForItemAtIndexPath 中返回 CGSize 的奇怪错误

ios - 以编程方式将 UILabels 快速添加到集合单元中