首页 > 解决方案 > 不知道为什么单元格没有出现并且没有错误

问题描述

我通过代码而不是自动布局编写设计,因此当我更改集合视图的背景颜色时,我用一个单元格绘制集合视图,它改变了但单元格没有出现。

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
         window = UIWindow(frame: UIScreen.main.bounds)         
       window?.makeKeyAndVisible()

        let layout =  UICollectionViewLayout()

        window?.rootViewController = UINavigationController(rootViewController: ViewController(collectionViewLayout: layout) )


        return true
    }

import UIKit

class ViewController:UICollectionViewController {
    var cell : UICollectionViewCell?
    override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.title = "Home"

        collectionView?.backgroundColor = UIColor.white

        collectionView?.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cellId")



        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath)
            cell?.backgroundColor = UIColor.red
            return cell!
    }

        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 5
    }


    }
}

没有错误,但单元格没有出现。

标签: iosswiftuicollectionview

解决方案


使用UICollectionViewLayout需要您手动管理布局属性,这可能需要一些工作,并且需要稍微深入了解幕后UICollectionView工作原理。

实现您想要做的最简单的方法是将布局更改为UICollectionViewFlowLayout

在您的 AppDelegate 中,更改

let layout =  UICollectionViewLayout()

let layout = UICollectionViewFlowLayout()

我可能还建议通过删除以下代码行来避免在控制器中捕获对单元格的引用:

var cell : UICollectionViewCell?

因此,完全修改您的代码将导致:

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        window = UIWindow(frame: UIScreen.main.bounds)         
        window?.makeKeyAndVisible()
        let layout = UICollectionViewFlowLayout()
        window?.rootViewController = UINavigationController(rootViewController: ViewController(collectionViewLayout: layout) )
        return true
    }
}

class ViewController: UICollectionViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.title = "Home"
        collectionView?.backgroundColor = UIColor.white
        collectionView?.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cellId")
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath)
        cell.backgroundColor = UIColor.red
        return cell
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 5
    }
}

推荐阅读