试图从故事板中跳出来。我正在尝试将两个UIViewControllers放入视图中,并水平滚动。
首先,我进入app 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
}
}
}
我的应用因nil布局参数而终止。我错过了什么构建时没有警告。
答案 0 :(得分:1)
您不能在没有参数的情况下初始化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
}()
答案 1 :(得分:1)
只需将UICollectionViewFlowLayout添加到您的集合视图
即可 public var collectionViewShirts : UICollectionView{
let layout = UICollectionViewFlowLayout()
let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
collectionView.delegate = self
collectionView.dataSource = self
return collectionView
}
public var collectionViewPants :UICollectionView{
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = UICollectionViewScrollDirection.horizontal
let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
collectionView.delegate = self
collectionView.dataSource = self
return collectionView
}