UICollectionView 以编程方式
UICollectionView Programmatically
我在故事板中创建了自己的 window,如下所示:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = UINavigationController(rootViewController: HomeController())
return true
}
当我的 class 是 UIViewController 的子class 时,这实际上工作正常。
class HomeController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
view.backgroundColor = UIColor.red
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
现在当我用 UICollectionViewController 子class 我的 class:
class HomeController: UICollectionViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
Collectionview.backgroundColor = UIColor.red
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
在运行时它给我一个错误提示:
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: 'UICollectionView must be
initialized with a non-nil layout parameter'
*** First throw call stack:
据我所知,它要求我为 UICollectionView 提供布局。如果我是 wrong.So,请在此处更正我,我试过这个-:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
let layout = UICollectionViewFlowLayout()
window?.rootViewController = UINavigationController(rootViewController: HomeController(UICollectionViewLayout: layout))
return true
}
但仍然以错误结束:
Argument labels 'UICollectionViewLayout' do not match any available
overloads.
我是否使用了错误的 swift3 语法?如果是,解决这个问题的正确方法是什么。
您需要在 UICollectionViewController
中实施 UICollectionViewFlowLayout
。
勾选这个Link
您必须使用 initWithFrame:collectionViewLayout:
来初始化您的 UICollectionView
并且您必须传递一个非零值 UICollectionViewLayout object
。
您需要使用 UICollectionViewController
的指定初始化器,如下所示。
let layout = UICollectionViewLayout()
let homeViewController = HomeController(collectionViewLayout: layout)
window?.rootViewController = UINavigationController(rootViewController: homeViewController)
除此之外,您还需要修改 HomeViewController
以实现 UICollectionViewDataSource
所需的方法。
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Register cell classes
self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
// Do any additional setup after loading the view.
}
// MARK: UICollectionViewDataSource
override func numberOfSections(in collectionView: UICollectionView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 0
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
return 0
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
// Configure the cell
return cell
}
我在故事板中创建了自己的 window,如下所示:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = UINavigationController(rootViewController: HomeController())
return true
}
当我的 class 是 UIViewController 的子class 时,这实际上工作正常。
class HomeController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
view.backgroundColor = UIColor.red
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
现在当我用 UICollectionViewController 子class 我的 class:
class HomeController: UICollectionViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
Collectionview.backgroundColor = UIColor.red
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
在运行时它给我一个错误提示:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter' *** First throw call stack:
据我所知,它要求我为 UICollectionView 提供布局。如果我是 wrong.So,请在此处更正我,我试过这个-:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
let layout = UICollectionViewFlowLayout()
window?.rootViewController = UINavigationController(rootViewController: HomeController(UICollectionViewLayout: layout))
return true
}
但仍然以错误结束:
Argument labels 'UICollectionViewLayout' do not match any available overloads.
我是否使用了错误的 swift3 语法?如果是,解决这个问题的正确方法是什么。
您需要在 UICollectionViewController
中实施 UICollectionViewFlowLayout
。
勾选这个Link
您必须使用 initWithFrame:collectionViewLayout:
来初始化您的 UICollectionView
并且您必须传递一个非零值 UICollectionViewLayout object
。
您需要使用 UICollectionViewController
的指定初始化器,如下所示。
let layout = UICollectionViewLayout()
let homeViewController = HomeController(collectionViewLayout: layout)
window?.rootViewController = UINavigationController(rootViewController: homeViewController)
除此之外,您还需要修改 HomeViewController
以实现 UICollectionViewDataSource
所需的方法。
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Register cell classes
self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
// Do any additional setup after loading the view.
}
// MARK: UICollectionViewDataSource
override func numberOfSections(in collectionView: UICollectionView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 0
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
return 0
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
// Configure the cell
return cell
}