2017-05-22 31 views
0

我設置了一個非常自定義的集合視圖,並正確地設置了一切我不斷收到此錯誤'UICollectionView必須使用非零布局參數初始化'。這是我的代碼。我已經深入研究了我的代碼,並確保我的身邊沒有問題,但堆棧溢出解決方案無法幫助我解決此問題。如果有人能幫助我,我將不勝感激。集合視圖佈局是零,我試過了一切

class ProducttableViewController: UICollectionViewController, CLLocationManagerDelegate, UICollectionViewDelegateFlowLayout { 
    var createdat = [Date]() 

    var datasource : [PFObject] = [] { 
     didSet { 
      self.collectionView?.reloadData() 
     } 
    } 

    let locationManager = CLLocationManager() 
    private var locManager = CLLocationManager() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     self.collectionView?.delegate = self 
     self.collectionView?.dataSource = self 
     let layout = BouncyLayout() 
     layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10) 
     layout.itemSize = CGSize(width: 111, height: 111) 
     self.collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout) 
     self.collectionView?.register(ProductCollectionViewCell.self, forCellWithReuseIdentifier: "PoductCollectionViewIdentifier") 
     locManager.delegate = self 
     locManager.requestWhenInUseAuthorization() 
     locManager.distanceFilter = 50 
     self.locationManager.requestAlwaysAuthorization() 
     self.locationManager.requestWhenInUseAuthorization() 
     self.collectionView?.backgroundColor = UIColor.white 
     setup() 
     if CLLocationManager.locationServicesEnabled() { 
      locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters 
     } 
     // Do any additional setup after loading the view. 
    } 

    override func viewWillAppear(_ animated: Bool) { 
     super.viewWillAppear(animated) 
     self.collectionView?.reloadData() 
    } 

    func setup() { 
     //query 
     let query = PFQuery(className: "hello") 
     query.order(byDescending: "createdAt") 
     query.includeKey("User") 
     query.findObjectsInBackground { (objects, error) in 
      if error == nil { 
       self.datasource = objects! 
       for object in objects!{ 
        self.createdat.append(object["createdAt"] as! Date) 
       } 
       self.collectionView?.reloadData() 
      } else { 
      } 
     } 
    } 

    private func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) { 
     switch status { 
     case .notDetermined: 
      locationManager.requestAlwaysAuthorization() 
      break 
     case .authorizedWhenInUse: 
      locationManager.startUpdatingLocation() 
      break 
     case .authorizedAlways: 
      locationManager.startUpdatingLocation() 
      break 
     case .restricted: 
      // restricted by e.g. parental controls. User can't enable Location Services 
      break 
     case .denied: 
      // user denied your app access to Location Services, but can grant access from Settings.app 
      break 
     default: 
      break 
     } 
    } 

    override func numberOfSections(in collectionView: UICollectionView) -> Int { 
     return 1 
    } 

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return self.datasource.count 
    } 

    override func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { 
     UIView.animate(withDuration: 1.8, delay: 0.0, usingSpringWithDamping: 0.0, initialSpringVelocity: 3.1, options: UIViewAnimationOptions.curveEaseInOut, animations: ({ 

     }), completion: nil) 
    } 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { 
     return UIEdgeInsetsMake(0, 0, 0, 0) 
    } 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
     return CGSize(width: UIScreen.main.bounds.width, height: 206) 
    } 

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PoductCollectionViewIdentifier", for: indexPath as IndexPath) as! ProductCollectionViewCell 

     cell.object = self.datasource[indexPath.row] 

     return cell 
    } 
} 
+0

採取通知。 – t4nhpt

+0

你的建議沒有解決問題 – john

回答

0

有同樣的問題。完成了設置佈局viewDidLayoutSubviews。嘗試,希望它有幫助。祝你好運!

+0

你的建議沒有解決問題 – john

0

看起來你正在初始化一個零框架的集合視圖。因此,collectionViewLayout不會被添加,我認爲這是零。

嘗試改變: self.collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)

到:

self.collectionView = UICollectionView(frame: view.frame, collectionViewLayout: layout)(或東西是不爲零)

+0

這沒有解決問題 – john

0

如果你已經繼承了UICollectionViewController,你不需要重新初始化的的CollectionView,因爲它已經有了。你只需要設置佈局手動

collectionView?.collectionViewLayout = layout 
相關問題