UICollectionView以编程方式 [英] UICollectionView Programmatically

查看:82
本文介绍了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以编程方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆