不知道为什么单元格不出现并且没有错误

时间:2019-07-22 18:50:51

标签: ios swift uicollectionview

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

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
    }


    }
}

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

4 个答案:

答案 0 :(得分:1)

使用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
    }
}

答案 1 :(得分:0)

尝试这样重写类

    class ViewController:UICollectionViewController {
            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 {
          let  cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath)
            cell.backgroundColor = UIColor.red
            return cell
    }

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

单元格常量不应该属于您的类,它必须在collectionviewDatasource方法中,并且它不是可选的,因为collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath)返回

  

UICollectionViewCell

不是可选的

  

UICollectionView?

不属于类的单元格的问题是UICollectionView是Refence类型,因此每一行将具有对同一单元格的引用。正如@rmady建议:主要原因是不要将数据源方法放在override func ViewDidLoad(){

答案 2 :(得分:0)

任何人都可以提供帮助的最新更新

  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
}

}

答案 3 :(得分:-1)

主要问题是您只是将集合视图委托的方法放到了 numberOfItemsInSection方法上的cellForItemAt indexPathviewDidLoad。这两个方法应该不属于viewDidLoad方法。

请检查以下代码

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

希望这可以解决此问题。