试图从 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/