Realm Platform 数据未使用 Storyboard 加载到 TableView - Swift 4
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: [=11=], section: 0) }),
with: .automatic)
tableView.deleteRows(at: deletions.map({ IndexPath(row: [=11=], section: 0)}),
with: .automatic)
tableView.reloadRows(at: modifications.map({ IndexPath(row: [=11=], 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()
...
大家好,这是我的问题,我一直在关注 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: [=11=], section: 0) }),
with: .automatic)
tableView.deleteRows(at: deletions.map({ IndexPath(row: [=11=], section: 0)}),
with: .automatic)
tableView.reloadRows(at: modifications.map({ IndexPath(row: [=11=], 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()
...