![](/img/trans.png)
[英]'UICollectionView must be initialized with a non-nil layout parameter' - Swift 4
[英]UICollectionView must be initialized with a non-nil layout parameter. error in swift with UICollectionView
嗨,大家好,我收到以下错误-UICollectionView必须使用非nil布局参数初始化
适用于PopularShotsCollectionViewController-的代码:
import UIKit
private let reuseIdentifier = "Cell"
class PopularShotsCollectionViewController: UICollectionViewController {
private var shots : [Shot] = [Shot](){
didSet{
self.collectionView?.reloadData()
}
}
var API_URl = Config.SHOT_URL
var shotPages = 1
private let leftAndRightPaddings : CGFloat = 32.0
private let numberOFItemsPerRow : CGFloat = 3.0
private let heightAdjustMent : CGFloat = 30.0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let width = (CGRectGetWidth(collectionView!.frame) - leftAndRightPaddings) / numberOFItemsPerRow
let layout = collectionViewLayout as! UICollectionViewFlowLayout
layout.itemSize = CGSizeMake(width, width + heightAdjustMent)
let popularShot = PopularShotsCollectionViewController()
popularShot.title = "Popular"
popularShot.API_URl = Config.POPULAR_URL
popularShot.loadShots()
}
func loadShots(){
DribbleObjectHandler.getShots(API_URl) { (shots) -> Void in
self.shots = shots
}
let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: Selector("refreshInvoked:"), forControlEvents: UIControlEvents.ValueChanged)
collectionView?.addSubview(refreshControl)
}
func refreshInvoked(sender : AnyObject){
sender.beginRefreshing()
collectionView?.reloadData()
sender.endRefreshing()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: UICollectionViewDataSource
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
return shots.count
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! PopularShotsCollectionViewCell
// Configure the cell
let shot = shots[indexPath.row]
cell.imageView.sd_setImageWithURL(NSURL(string : shot.imageUrl))
if shots.count - 1 == indexPath.row && shotPages < 5{
shotPages++
print(shotPages)
let url = API_URl + "&page=" + String(shotPages)
DribbleObjectHandler.getShots(url, callback: { (shots) -> Void in
for shot in shots {
self.shots.append(shot)
}
})
}
return cell
}
}
如果我在此行之后尝试调试任何代码,它将自动崩溃而无需告知
我已经检查了Main.storyboard,什么也没有,这一切都应该将类连接到视图,委托和数据源也是如此
请记住,我只有12岁,对Swift来说还很陌生。任何帮助将不胜感激
在此先感谢Aryan Kashyap
我不明白为什么要在viewdidload中创建自身的新实例:
let popularShot = PopularShotsCollectionViewController()
popularShot.title = "Popular"
popularShot.API_URl = Config.POPULAR_URL
popularShot.loadShots()
我认为这可能是您陷入循环的问题?
你不能只是使用...
self.title = "Popular"
self.API_URl = Config.POPULAR_URL
self.loadShots()
加载collectionViewController时在viewDidLoad中...
看看那有什么不同
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.