UICollectionView以编程方式

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


    }

在运行时,它会给我一个错误,告诉我:
由于意外异常而终止应用程序
“NSInvalidArgumentException”,原因:“UICollectionView必须是
用非nil布局参数初始化'
***第一个抛出调用堆栈:
据我所知,它要求我给出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()
        let layout = UICollectionViewFlowLayout()
        window?.rootViewController = UINavigationController(rootViewController: HomeController(UICollectionViewLayout: layout))
        return true
    }

但最终还是出现了一个错误:
参数标签“UICollectionViewLayout”与任何可用的
超载。
我是否使用了错误的swift3语法?如果是,解决这个问题的正确方法是什么。

最佳答案

您需要使用指定的初始化器,如下所示。

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

除此之外,您还需要修改UICollectionViewController以实现HomeViewController所需的方法。
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
    }

关于ios - UICollectionView以编程方式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43091609/

10-10 20:41