2016-03-03 106 views
1

我正在构建一个具有自定义collectionCell的UICollectionView。但我得到的错误:UICollectionView崩溃,因为布局参数是“无”..但它不是 - Swift

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter'

但你可以从代码中看到,我很明确地创建布局和初始化集合视图时,把它当作一个PARAM。

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

} 
+0

您是否尝试过干净重建项目初始化工作得很好? (Cmd + Shift + K) – shpasta

+0

yes no success .. – broderickga

+0

您的视图控制器继承自'UICollectionViewController',因此您不需要初始化新的集合视图并将其分配给'self.collectionView'。尝试删除该行并查看会发生什么。 – AdamPro13

回答

0

尝试更换你的代码为:

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

雨燕3.0

我所面临的应用程序,一旦崩溃了同样的问题随着它启动。当像下面

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

我使用这么用的autoLayout CGRect.zero帧

相关问题