ios - UICollectionView 消失

标签 ios swift xcode uicollectionview

我对 Swift 还很陌生。我的问题是我的 UICollectionView 正在消失。 在 Xcode 中,它显示一切就绪,但当我在模拟器或设备上启动时,它消失了,只剩下导航栏和标签栏。

有谁知道是什么原因造成的或如何解决这个问题? 干杯!

enter image description here

enter image description here

这是我的代码:

class User: 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
}

最佳答案

您正在使用生成的类并且您有部分数 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()
}

关于ios - UICollectionView 消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51723599/

相关文章:

iphone - 在 Xcode 中,我们如何删除旧的物理设备?

ios - 带有完成处理程序的 storageReference.downloadURL ... url 不断返回 nil

ios - 由于名称为 'NSInternalInconsistencyException' 的未捕获异常 'OverlayView' 而终止应用程序

swift - HTTP POST 请求返回尝试从对象[0] 插入 nil 对象?

xcode - 除非用户登录,否则钥匙串(keychain)不会从 Jenkins 脚本解锁

ios - 立即导出 IPA xCode 7 崩溃

ios - 由变量组成的 UIColor

ios - 使用 NSAttributedString 覆盖 HTML 属性

ios - SwiftUI 中的 ForEach 在部分之间交换 View (从而重新使用它们),而不是更新数据并尊重 View 修饰符

ios 开发 - 如何做 JSON 解析器?