UICollectionView消失

时间:2018-08-07 09:40:31

标签: ios swift xcode uicollectionview

我对Swift非常陌生。我的问题是我的UICollectionView消失了。 在Xcode中,它显示一切就绪,但是当我在模拟器或设备上启动时,它仅在导航栏和选项卡栏的左侧消失。

有人知道这是什么原因或如何解决? 干杯!

enter image description here

enter image description here

这是我的代码:

  

类用户:UICollectionViewController {

override func viewDidLoad() {
    super.viewDidLoad()
}


override func numberOfSections(in collectionView: UICollectionView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 0
}


override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of items
    return 0
}

override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "Header", for: indexPath) as! UserHeaderView
    header.userEmail.text = PFUser.current()!.email
    header.userChosenName.text = PFUser.current()!.object(forKey: "nickname") as? String
    header.profilePicture.layer.cornerRadius = header.profilePicture.frame.size.width/2
    header.profilePicture.clipsToBounds = true
    let query = PFUser.current()?.object(forKey: "profilePicture") as! PFFile
    query.getDataInBackground { (image, error) in
        if error == nil {
        header.profilePicture.image = UIImage(data: image!)
        }
        else {
            SVProgressHUD.showError(withStatus: "Something's Wrong...")
        }
    }
    return header
}

1 个答案:

答案 0 :(得分:1)

您正在使用生成的类,并且具有0部分的数目和0行的数目,因此您必须根据希望它们显示的计数来更改这些部分

override func viewDidLoad() {
   super.viewDidLoad()
}


override func numberOfSections(in collectionView: UICollectionView) -> Int {
    // The number of sections you have to provide 
    return 1
}


override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    // Number of Items in this section at least it should be 1
    return 1
}

override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    let header = collectionView.dequeueReusableSupplementaryView(ofKind:  UICollectionElementKindSectionHeader, withReuseIdentifier: "Header", for: indexPath) as! UserHeaderView
    header.userEmail.text = PFUser.current()!.email
    header.userChosenName.text = PFUser.current()!.object(forKey: "nickname") as? String
    header.profilePicture.layer.cornerRadius = header.profilePicture.frame.size.width/2
header.profilePicture.clipsToBounds = true
   let query = PFUser.current()?.object(forKey: "profilePicture") as! PFFile
query.getDataInBackground { (image, error) in
    if error == nil {
    header.profilePicture.image = UIImage(data: image!)
    }
    else {
        SVProgressHUD.showError(withStatus: "Something's Wrong...")
    }
}
    return header
}

编辑: 您应该具有一个cellForItemAt indexPath函数,否则应用程序将崩溃。这是该功能的示例

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    // this will return an empty cell where you're app will not crash
    // but you have to create a cell and populate some data to the cell
    return UICollectionViewCell()
}