我設置了一個非常自定義的集合視圖,並正確地設置了一切我不斷收到此錯誤'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
}
}
採取通知。 – t4nhpt
你的建議沒有解決問題 – john