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

标签 ios swift uicollectionview

大家好,我收到以下错误 - UICollectionView 必须使用非零布局参数初始化

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 ,委托(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/

相关文章:

c# - GPUImage 和 Monotouch 绑定(bind)或替代方案

ios - 启用分页的 UICollectionView

ios - UICollectionViewCell 阴影与装饰 View 不一致

ios - 防止调用 API 获取时间范围内的数据

swift - 更新 UIAlertAction 处理程序中的 UI

ios - Swift UICollectionViewCell didSelectItemAt 不在单元格上打印标签名称

ios - 使用团队供应配置文件的临时分发

ios - 从后台发送 UILocationNotification

c# - 猜数字游戏还没开始就结束了

ios - 使用 Swift 4 在 ScrollView 中嵌入 AdMob 广告