试图从故事板跳船。我试图将两个UIViewControllers放入视图中,并水平滚动。

所以首先,我进入应用程序委托

    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
    }
  }
}

我的应用程序由于nil布局参数而终止。我想念什么。构建时没有警告。

最佳答案

您不能初始化不带参数的UICollectionView。您需要提供collectionViewLayout参数,以便收集视图知道如何安排其内容。

而是创建一个布局对象,并使用它来初始化集合视图。在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中以编程方式在商店中添加两个UICollectionViews,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45850181/

10-13 03:50