我设置了一个非常自定义的 Collection View 并正确设置了所有内容,但我不断收到此错误“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
}
}
最佳答案
有同样的问题。最终在 viewDidLayoutSubviews
上设置布局。尝试一下,希望有帮助。祝你好运!
关于ios - Collection View 布局为零,我已经尝试了一切,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44104656/