ios - Collection View 布局为零,我已经尝试了一切

标签 ios objective-c swift xcode swift3

我设置了一个非常自定义的 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/

相关文章:

ios - 应用程序在 iOS5 模拟器和分析器上崩溃

ios - 当键盘出现时如何阻止 tableView 滚动?

arrays - 从值为空字符串的数组中删除字典(使用高阶函数)

ios - 带有 VIPER 的 iOS 中的 BLE

iphone - 类似 Instagram 评论的富 TextView

iphone - 使用 iphone 摄像头检测和识别数字

objective-c - NS_SWIFT_NAME 基本名称验证错误

swift - 在 Realm 移动平台上从客户端创建用户

ios - 如何在 iOS 7 和 iOS 8 中使用命名空间支持 Core Data?

ios - 在 PopOverViewController 中显示 UISplitviewController