gpt4 book ai didi

ios - UICollectionView 必须使用非零布局参数进行初始化。 UICollectionView 快速出错

转载 作者:可可西里 更新时间:2023-11-01 01:01:53 27 4
gpt4 key购买 nike

大家好,我收到以下错误 -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
}
}

This is Where The App Crashes

You can have a look at the log also

如果我尝试调试此行之后的任何代码,它会自动崩溃而不通知

我已经检查了 Main.storyboard,什么都没有,这应该是类与 View Hook 的一切,委托(delegate)和数据源也是如此

请记住,我只有 12 岁,对 Swift 还很陌生。非常感谢任何帮助

提前致谢雅利安卡夏普

最佳答案

我不明白你为什么要在 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 中。

看看有没有区别

关于ios - UICollectionView 必须使用非零布局参数进行初始化。 UICollectionView 快速出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34485141/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com