简体   繁体   English

必须使用非nil布局参数初始化UICollectionView。 迅速与UICollectionView错误

[英]UICollectionView must be initialized with a non-nil layout parameter. error in swift with UICollectionView

Hi guys so I am getting the following error - UICollectionView must be initialized with a non-nil layout parameter 嗨,大家好,我收到以下错误-UICollectionView必须使用非nil布局参数初始化

The code For PopularShotsCollectionViewController-: 适用于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 您也可以看看日志

And if i try to debug any code after this line it automatically crashes without telling 如果我在此行之后尝试调试任何代码,它将自动崩溃而无需告知

I have checked in Main.storyboard and nothing and everything this is like it should be the classes are hooked up to the view so is the delegate and the data source 我已经检查了Main.storyboard,什么也没有,这一切都应该将类连接到视图,委托和数据源也是如此

Please Bear in Mind i am only 12 and fairly new to Swift.. Any help will be really appreciated 请记住,我只有12岁,对Swift来说还很陌生。任何帮助将不胜感激

Thanks In Advance Aryan Kashyap 在此先感谢Aryan Kashyap

I don't understand why you create a new instance of itself in viewdidload: 我不明白为什么要在viewdidload中创建自身的新实例:

    let popularShot = PopularShotsCollectionViewController()
    popularShot.title = "Popular"
    popularShot.API_URl = Config.POPULAR_URL
    popularShot.loadShots()

I think this might be your issue where it gets stuck in a loop? 我认为这可能是您陷入循环的问题?

Can't you just use... 你不能只是使用...

self.title = "Popular"
self.API_URl = Config.POPULAR_URL
self.loadShots()

...in viewDidLoad when the collectionViewController loads. 加载collectionViewController时在viewDidLoad中...

See if that makes a difference 看看那有什么不同

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 &#39;UICollectionView必须使用非nil布局参数初始化&#39;-Swift 4 - 'UICollectionView must be initialized with a non-nil layout parameter' - Swift 4 UiCollectionView必须使用非nil布局参数初始化Swift 3 - UiCollectionView must be initialized with a non-nil layout parameter Swift 3 错误:UICollectionView必须使用非nil布局参数初始化 - Error: UICollectionView must be initialized with a non-nil layout parameter 即使在UICollectionView文件中定义了参数,也必须在Swift中使用非nil布局参数初始化UICollectionView - UICollectionView must be initialized with a non-nil layout parameter in Swift, even though the parameter is defined in the UICollectionView file “必须使用非零布局参数初始化 UICollectionView”错误 - 'UICollectionView must be initialized with a non-nil layout parameter' bug “ UICollectionView必须使用非null布局参数初始化” - 'UICollectionView must be initialized with a non-nil layout parameter' UICollectionView:必须使用非 nil 布局参数初始化 - UICollectionView: must be initialized with a non-nil layout parameter 由于未捕获的异常&#39;NSInvalidArgumentException&#39;而终止应用程序,原因:&#39;UICollectionView必须使用非nil布局参数初始化 - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter 使用JSQMessagesCollectionView时,“ UICollectionView必须使用非nil布局参数初始化” - 'UICollectionView must be initialized with a non-nil layout parameter' while using JSQMessagesCollectionView 使用多个UICollectionView时,出现错误,“ ...必须使用非nil布局初始化” - With Multiple UICollectionView's, getting error, “… must be initialized with non-nil layout”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM