UICollectionView以编程方式

编程入门 行业动态 更新时间:2024-10-11 17:27:12
本文介绍了UICollectionView以编程方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在故事板中创建了我自己的窗口,如下所示:

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 }

当我的类继承 UIViewController时,这实际上工作正常。

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

在运行时它给我一个错误说:

At runtime it gives me an error saying:

由于未捕获的异常而终止应用'NSInvalidArgumentException',原因:'UICollectionView必须是初始化为非零布局参数' ***第一次调用堆栈:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter' *** First throw call stack:

在我的理解中我要求我为 UICollectionView 提供布局。如果我错了,请在这里告诉我。我试过这个 - :

In my understanding it is asking me to give layout for UICollectionView.Correct me here if I am wrong.So I tried this-:

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 }

但仍然结束有错误:

参数标签'UICollectionViewLayout'与任何可用的重载都不匹配。

Argument labels 'UICollectionViewLayout' do not match any available overloads.

我使用了错误的 swift3语法吗?如果是,解决这个问题是正确的。

Am I using a wrong swift3 syntax? If yes what is correct to solve out this issue.

推荐答案

你需要使用 UICollectionViewController 指定的intialiser如下。

You need to use UICollectionViewController's designated intialiser as below.

let layout = UICollectionViewLayout() let homeViewController = HomeController(collectionViewLayout: layout) window?.rootViewController = UINavigationController(rootViewController: homeViewController)

以及那么,您需要修改 HomeViewController 来实现 UICollectionViewDataSource 所需的方法。

Along with that, you will need to modify you HomeViewController to implement UICollectionViewDataSource required methods.

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 }

更多推荐

UICollectionView以编程方式

本文发布于:2023-11-02 09:55:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552068.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方式   UICollectionView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!