Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/37.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 UICollectionView在布局参数为“时崩溃”;无;。。但是它';他不是斯威夫特_Ios_Iphone_Swift - Fatal编程技术网

Ios UICollectionView在布局参数为“时崩溃”;无;。。但是它';他不是斯威夫特

Ios UICollectionView在布局参数为“时崩溃”;无;。。但是它';他不是斯威夫特,ios,iphone,swift,Ios,Iphone,Swift,我正在使用自定义collectionCell构建UICollectionView。但我得到了一个错误: ***由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“UICollectionView必须使用非nil布局参数初始化” 但正如您从代码中看到的,我非常明确地创建了布局,并在初始化集合视图时将其作为参数传递 import UIKit private let reuseIdentifier = "Cell" class ProfileViewCo

我正在使用自定义collectionCell构建UICollectionView。但我得到了一个错误:

***由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“UICollectionView必须使用非nil布局参数初始化”

但正如您从代码中看到的,我非常明确地创建了布局,并在初始化集合视图时将其作为参数传递

import UIKit

private let reuseIdentifier = "Cell"

class ProfileViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

override func viewDidLoad() {
    super.viewDidLoad()
    sentMngr.retrieveSent()
    let flow = UICollectionViewFlowLayout()
    flow.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
    flow.itemSize = CGSize(width: 90, height: 90)
    flow.scrollDirection = UICollectionViewScrollDirection.Vertical

    self.collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: flow)
    self.collectionView!.dataSource = self
    self.collectionView!.delegate = self
    self.collectionView!.backgroundColor = UIColor.blackColor()
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Register cell classes
    self.collectionView!.registerClass(CollectionCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    self.view.addSubview(self.collectionView!)

    // Do any additional setup after loading the view.
}
自定义单元格代码:

import UIKit

class CollectionCell: UICollectionViewCell {
var imageView: UIImageView!
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
    // Drawing code
}
*/
override init(frame: CGRect) {
    super.init(frame: frame)

    imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height*2/3))
    imageView.contentMode = UIViewContentMode.ScaleAspectFit
    self.addSubview(imageView)

}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}

尝试将代码替换为:

import UIKit

private let reuseIdentifier = "Cell"

class ProfileViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

override func viewDidLoad() {
    super.viewDidLoad()

    sentMngr.retrieveSent()

    self.collectionView!.backgroundColor = UIColor.blackColor()
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Register cell classes
self.collectionView!.registerClass(CollectionCell.self, forCellWithReuseIdentifier: reuseIdentifier)
   self.view.addSubview(self.collectionView!)

  // Do any additional setup after loading the view.
}

尝试将代码替换为:

import UIKit

private let reuseIdentifier = "Cell"

class ProfileViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

override func viewDidLoad() {
    super.viewDidLoad()

    sentMngr.retrieveSent()

    self.collectionView!.backgroundColor = UIColor.blackColor()
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Register cell classes
self.collectionView!.registerClass(CollectionCell.self, forCellWithReuseIdentifier: reuseIdentifier)
   self.view.addSubview(self.collectionView!)

  // Do any additional setup after loading the view.
}

Swift 3.0

我遇到了同样的问题,应用程序一启动就崩溃了。在如下初始化时工作正常

var alarmCollectionView=UICollectionView(帧:CGRect.zero,collectionViewLayout:UICollectionViewFlowLayout.init())


我使用的是自动布局,因此在帧中使用了CGRect.zero

我遇到了同样的问题,应用程序一启动就崩溃了。在如下初始化时工作正常

var alarmCollectionView=UICollectionView(帧:CGRect.zero,collectionViewLayout:UICollectionViewFlowLayout.init())


我正在使用自动布局,所以在框架中使用了CGRect.zero

您是否尝试过清理和重建项目?(Cmd+Shift+K)视图控制器继承自
UICollectionViewController
,因此不需要初始化新的集合视图并将其分配给
self.collectionView
。试着删除那一行,看看会发生什么。对不起,我不明白。哪一行?我需要构建一个CollectionView来显示..您是否尝试过清理和重建项目?(Cmd+Shift+K)视图控制器继承自
UICollectionViewController
,因此不需要初始化新的集合视图并将其分配给
self.collectionView
。试着删除那一行,看看会发生什么。对不起,我不明白。哪一行?我需要建立一个CollectionView来显示。。