【问题标题】:Horizontal UICollectionView with CoreData带有 CoreData 的水平 UICollectionView
【发布时间】:2018-11-09 18:50:12
【问题描述】:

我正在尝试水平 UICollectionView 打印出 CoreData 条目,当我输入数据并尝试查看数据时,我没有收到任何错误,但数据不会在 UICollectionView 中打印出来。在 CoreData 中输入数据不是问题。下面是我在 UICollectionView 中获取 CoreData 并打印出来的代码,我哪里出错了?

class HistoryVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    var QBHistory = [QB]()

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return QBHistory.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "QBCollectionViewCell", for: indexPath) as! QBCollectionViewCell
        do{
            QBHistory = try context.fetch(QB.fetchRequest())
            for each in QBHistory {
                cell.QBName.text = each.qbName
                cell.QBPotential.text = each.qbPotential
                cell.QBValue.text = each.qbValue
                cell.QBBust.text = String(each.qbBust)
                cell.QBRound.text = each.qbRound
            }

        } catch {
            print("oh no it broke")
        }
        return cell
    }
}


【问题讨论】:

    标签: ios swift core-data uicollectionview


    【解决方案1】:

    问题是numberOfItemsInSection中的这一行:

    return QBHistory.count
    

    那将是零,所以你有零个单元格。

    你需要移动

    QBHistory = try context.fetch(QB.fetchRequest())
    

    进入viewDidLoad,以便它发生在你的集合视图数据源查询方法被调用之前。

    【讨论】:

      猜你喜欢
      • 2013-06-20
      • 1970-01-01
      • 2015-09-27
      • 1970-01-01
      • 2013-10-18
      • 2019-11-04
      • 2016-09-17
      相关资源
      最近更新 更多