簡體   English   中英

Realm Platform數據未使用Storyboard-Swift 4加載到TableView中

[英]Realm Platform Data not loading into TableView with Storyboard- Swift 4

大家好,這是我的問題,我一直在Realm Platform待辦事項列表應用程序中關注,但是我正在根據自己的需求對其進行自定義。 該項目最初是用沒有情節提要構建的,但是我想使用情節提要。 我無法獲得tableView來顯示來自領域平台的數據。 數據將添加內容寫入平台,但不會傳遞到tableView中。 有什么建議么?

謝謝,

油菜。

這是代碼:

import UIKit
import RealmSwift
import SVProgressHUD
import SwipeCellKit

class HomeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet var tableView: UITableView!
    @IBOutlet weak var quickNotes: UITextView!

    //Create our Realm File
    let realm = try! Realm(configuration: SyncConfiguration.automatic())
    var items: Results<Items>?
    var notificationToken: NotificationToken?
    var subscriptionToken: NotificationToken?
    var subscription: SyncSubscription<Items>!
    var notes: Results<Notes>?

    override func viewDidLoad() {
        super.viewDidLoad()



        //Load our Realm Object into the view

        tableView.rowHeight = 70.0
        tableView.delegate = self
        tableView.dataSource = self
        tableView.reloadData()
        loadCategory()
        subscription = items?.subscribe(named: "my-projects")

        subscriptionToken = subscription.observe(\.state, options: .initial) { state in
            if state == .complete {

            } else {
                print("Subscription State: \(state)")
            }
        }


        notificationToken = items?.observe { [weak self] (changes) in
            guard let tableView = self?.tableView else { return }
            switch changes {
            case .initial:
                // Results are now populated and can be accessed without blocking the UI
                tableView.reloadData()
            case .update(_, let deletions, let insertions, let modifications):
                // Query results have changed, so apply them to the UITableView
                tableView.beginUpdates()
                tableView.insertRows(at: insertions.map({ IndexPath(row: $0, section: 0) }),
                                     with: .automatic)
                tableView.deleteRows(at: deletions.map({ IndexPath(row: $0, section: 0)}),
                                     with: .automatic)
                tableView.reloadRows(at: modifications.map({ IndexPath(row: $0, section: 0) }),
                                     with: .automatic)
                tableView.endUpdates()
            case .error(let error):
                // An error occurred while opening the Realm file on the background worker thread
                fatalError("\(error)")
            }
        }
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items?.count ?? 0
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "categoryCell", for: indexPath)

        cell.textLabel?.text = items?[indexPath.row].name ?? "There were no categories added"

        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        let item = items?[indexPath.row]
        let listItemsVC = CloudListViewController()
        listItemsVC.project = item
        performSegue(withIdentifier: "segueToCloudLists", sender: self)
        tableView.deselectRow(at: indexPath, animated: true)
    }

    //Add List Item to Database
    @IBAction func quickAdd(_ sender: UIBarButtonItem) {
        let alertController = UIAlertController(title: "Add New Project", message: "", preferredStyle: .alert)

        alertController.addAction(UIAlertAction(title: "Save", style: .default) { _ in
            let textField = alertController.textFields![0]
            let item = Items()
            item.name = textField.text ?? "Nothing was added"

            try! self.realm.write {
                self.realm.add(item)

                let user = self.realm.object(ofType: PermissionUser.self, forPrimaryKey: SyncUser.current!.identity!)!
                let permission = item.permissions.findOrCreate(forRole: user.role!)
                permission.canRead = true
                permission.canUpdate = true
                permission.canDelete = true
            }
        })
        alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel))
        alertController.addTextField() { textField in
            textField.placeholder = "New Item Text"
        }
        self.present(alertController, animated: true, completion: nil)
    }
}

我想您正在loadCategory函數中加載items (不能確定,因為您省略了該函數的定義)。

您需要將數據檢索到數據源結構(本例中的items )之后而不是在此之前調用tableView.reloadData()

...
loadCategory()
subscription = items?.subscribe(named: "my-projects")
tableView.reloadData()
...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM