【问题标题】:UICollectionView DisappearUICollectionView 消失
【发布时间】:2018-08-07 09:40:31
【问题描述】:

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

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

这是我的代码:

类用户: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
}

【问题讨论】:

  • 在这里分享您的代码,我们可以解决问题
  • 您是否为设计的每个项目设置了自动布局??
  • 您在 numberOfItemsInSection 和 numberOfSections 中返回 0 个项目,这就是您没有显示任何单元格的原因
  • 您将从 numberOfItemsInSection 和 numberOfSections 返回一些数字,然后您将能够看到单元格或 headerView
  • @MaheshDangar 这是我的代码,请看一下。干杯!

标签: ios swift xcode uicollectionview


【解决方案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()
}

【讨论】:

  • 感谢您这么快回复。我已按照您的指示将整数更改为 1,但是当我打开视图时应用程序崩溃了。原因:集合视图的数据源没有从 -collectionView:cellForItemAtIndexPath: 索引路径返回有效单元格 。你能帮我解决这个问题吗?
  • @zhangallan 看到编辑过的那里缺少你所拥有的功能,并阅读更多关于如何填充的信息
  • 解决了!!非常感谢您的时间和精力。我确实需要更多的经验和更多的东西来学习。但再次感谢,祝你有美好的一天!
猜你喜欢
  • 2023-03-25
  • 2012-11-01
  • 1970-01-01
  • 2013-12-04
  • 1970-01-01
  • 1970-01-01
  • 2014-01-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多