【问题标题】:Displaying realm data in UIKit在 UIKit 中显示领域数据
【发布时间】:2021-12-28 09:54:06
【问题描述】:

我在 UIKit 中使用领域。我设置了保存到领域,一切都显示在 MongoDB Realm Studio 中,但是当我希望这些数据显示在 UITableView 中时,什么都没有。 我写了一个扩展,好像没有错误,但是不明白为什么数据不显示。

extension ViewController: UITableViewDelegate, UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return spendingDBArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomTableViewCell
        
        let spending = spendingDBArray[indexPath.row]
        
        cell.recordCategory.text = spending.category 
        cell.recordMoney.text = "\(spending.cost)"
        
        print("work")
        
        switch spending.category {
        case "Home": cell.recordImage.image = #imageLiteral(resourceName: "Frame 4")
        case "Clothes": cell.recordImage.image = #imageLiteral(resourceName: "Frame 8")
        case "Connection": cell.recordImage.image = #imageLiteral(resourceName: "Frame 7")
        case "Auto": cell.recordImage.image = #imageLiteral(resourceName: "Frame 3")
        case "Pharmacy": cell.recordImage.image = #imageLiteral(resourceName: "Frame 2")
        case "Products": cell.recordImage.image = #imageLiteral(resourceName: "Frame 1")
        case "Cafe": cell.recordImage.image = #imageLiteral(resourceName: "Frame 6")
        case "Other": cell.recordImage.image = #imageLiteral(resourceName: "Frame 5")
        default:
            cell.recordImage.image = #imageLiteral(resourceName: "Rectangle 1")
        }
        return cell
    }
    
}

【问题讨论】:

    标签: ios swift uikit realm


    【解决方案1】:

    我通过以下方式获取 Realm 数据:

    lazy var yourvarname: Results<yourObjectClass> = { 
        self.realm.objects(yourObjectClass.self) }()
    
    cell.textlabel.text = yourvarname[indexPath.row].varinyourObjectClass
    

    不需要扩展。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多