集合视图布局是零,我已经尝试了一切

时间:2017-05-22 04:25:43

标签: ios objective-c swift xcode swift3

我设置了一个非常自定义的集合视图并正确设置所有内容我一直收到此错误'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
    }
}

4 个答案:

答案 0 :(得分:0)

有同样的问题。结束viewDidLayoutSubviews上的设置布局。试试,希望它有所帮助。祝你好运!

答案 1 :(得分:0)

看起来您正在使用零帧初始化集合视图。因此,不会添加collectionViewLayout,我认为这使它成为零。

尝试更改: self.collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)

到:

self.collectionView = UICollectionView(frame: view.frame, collectionViewLayout: layout)(或非零的东西)

答案 2 :(得分:0)

如果您已经继承了name1, name2 O , T O , N ,则不需要重新初始化collectionView,因为它已经有了。您只需手动设置布局

UICollectionViewController

答案 3 :(得分:0)

你的集合视图init应该调用/覆盖

super.init(collectionViewLayout: UICollectionViewLayout)

使用您的自定义布局。

请参阅https://developer.apple.com/documentation/uikit/uicollectionviewcontroller