Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Don';我不知道为什么该单元不';没有出现,就没有';不要犯错误_Ios_Swift_Uicollectionview - Fatal编程技术网

Ios Don';我不知道为什么该单元不';没有出现,就没有';不要犯错误

Ios Don';我不知道为什么该单元不';没有出现,就没有';不要犯错误,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我不是从autolayout通过代码编写设计的,所以当我更改集合视图的背景色时,我会用一个单元格绘制集合视图,它已更改,但单元格未显示 import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaun

我不是从autolayout通过代码编写设计的,所以当我更改集合视图的背景色时,我会用一个单元格绘制集合视图,它已更改,但单元格未显示

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
    }


    }
}

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

尝试像这样重写类

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
    }


    }
}
    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
    }
}
cell常量不应该是类的一部分,因为它必须在collectionviewDatasource方法中,而且它也不是可选的,因为
collectionView.dequeueReusableCell(withReuseIdentifier:“cellId”,for:indexPath)
返回

UICollectionViewCell

不是可选的

UICollectionView


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

使用
UICollectionViewLayout
需要手动管理布局属性,这可能需要一点工作,并且需要稍微深入了解
UICollectionView
在引擎盖下的工作方式

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

在AppDelegate中,更改

let layout =  UICollectionViewLayout()

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

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的
numberOfSections
方法?不要将集合视图委托方法放入
viewDidLoad
中。如果您的代码格式正确,问题就会很清楚。该单元格是原型单元格吗?如果是,您必须不注册该单元格。必须查看xcode,因为我不记得这一点,但为true,请退出队列ableCell反驳的是uitableviewCell而不是uitableviewCell?,谢谢。不要只发布代码答案。解释错误并解释此答案如何解决问题。这些信息应该出现在你的答案中,而不是评论中。希望意见不要完全错误,@rmaddyy你忘了提到最重要的部分-不要,但是
viewDidLoad
中的集合视图方法。好的,但是您在答案中的第一个注释是我推荐的,但是添加到了答案中。我认为这个答案还需要您放置
layout.estimatedItemSize=CGSize(宽度:宽度,高度:高度);layout.itemSize=CGSize(宽度:宽度,高度:高度)
,其中
width
height
是应用程序中任意位置定义的常量。在我运行此功能的游乐场中,它似乎不需要设置这些属性,因此理论上似乎不是绝对必需的。然而,在实践中,人们肯定希望按照上述方式或通过布局的dele来调整单元格的大小门方法。