必须使用非零布局参数初始化UICollectionView。使用UICollectionView在swift中出错

时间:2015-12-27 21:44:52

标签: 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,没有任何东西,这就像它应该是类被连接到视图所以是委托和数据源

请记住,我只有12岁,对Swift来说还是新手......任何帮助都会非常感激

先谢谢 雅利安卡什亚普

1 个答案:

答案 0 :(得分:3)

我不明白你为什么在viewdidload中创建自己的新实例:

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

我认为这可能是你陷入循环的问题吗?

你不能只使用......

UITableView
在collectionViewController加载时的viewDidLoad中。

看看是否有所作为