在 UIKit 中显示领域数据
Displaying realm data in UIKit
我在 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
}
}
我通过以下方式获取我的领域数据:
lazy var yourvarname: Results<yourObjectClass> = {
self.realm.objects(yourObjectClass.self) }()
cell.textlabel.text = yourvarname[indexPath.row].varinyourObjectClass
不需要延期。
我在 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
}
}
我通过以下方式获取我的领域数据:
lazy var yourvarname: Results<yourObjectClass> = {
self.realm.objects(yourObjectClass.self) }()
cell.textlabel.text = yourvarname[indexPath.row].varinyourObjectClass
不需要延期。