Home > Software engineering >  Displaying realm data in UIKit
Displaying realm data in UIKit

Time:11-18

I work with realm in UIKit. I set up saving to the realm, everything is displayed in MongoDB Realm Studio, but when I want this data to be shown in UITableView, there is nothing. I wrote an extension, there seem to be no errors, but I don't understand why the data is not displayed.

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
    }
    
}

CodePudding user response:

I get my Realm data by:

lazy var yourvarname: Results<yourObjectClass> = { 
    self.realm.objects(yourObjectClass.self) }()

cell.textlabel.text = yourvarname[indexPath.row].varinyourObjectClass

No extension is needed.

  • Related