如何获取 Realm 数据库中的名单并显示在我的刺激器上?
How to get the name list on Realm database and display on my stimulator?
这里我有一个 Realm 数据库,里面有一些数据,我想在我的 Stimulator 上显示它,但它显示了一些其他的东西。我的代码有什么问题?
这是我的Realm Database的数据,我也标出了我要显示的数据。
显示类似这样的刺激器。
这是我的 ViewController.swift 代码。
import UIKit
import RealmSwift
class ViewController: UIViewController,UITableViewDataSource { //UITableViewDataSource
@IBOutlet weak var mytableview: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let realm = try! Realm()
let theItem = realm.objects(Item.self).filter("itemid >= 1")
return theItem.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let realm = try! Realm()
let theItem = realm.objects(Item.self).filter("itemid >= 1")
print(theItem)
let cell = tableView.dequeueReusableCell(withIdentifier: "cell1")
//I suspect the problem is at here...
cell?.textLabel?.text = "\(theItem)"
return cell!
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
}
class Category: Object {
@objc dynamic var name: String?
@objc dynamic var caid: Int = 0
}
class Item: Object {
@objc dynamic var name: String?
@objc dynamic var itemid: Int = 0
@objc dynamic var cateid: Int = 0
}
您的问题是您需要从 Item
对象中获取字符串。试试
"\(theItem.name)"
。
func getNames() -> [String]{
let items = realm.objects(Item.self).filter("itemid >= 1").toArray(ofType: Item.self ) as [Item]
return items.map { [=10=].name }
}
extension Results {
func toArray<T>(ofType: T.Type) -> [T] {
var array = [T]()
for i in 0 ..< count {
if let result = self[i] as? T {
array.append(result)
}
}
return array
}
}
我已经找到了显示数据的方法。我只需要在我的代码中添加 indexPath.row
它就可以处理数据了。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let realm = try! Realm()
let theItem = realm.objects(Item.self).filter("itemid >= 1")
//I only add below this indexpath
let cellData = theItem[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "cell1")
//and change this part and it's done.
cell?.textLabel?.text = cellData.name
print(theItem)
return cell!
}
这里我有一个 Realm 数据库,里面有一些数据,我想在我的 Stimulator 上显示它,但它显示了一些其他的东西。我的代码有什么问题?
这是我的Realm Database的数据,我也标出了我要显示的数据。
显示类似这样的刺激器。
这是我的 ViewController.swift 代码。
import UIKit
import RealmSwift
class ViewController: UIViewController,UITableViewDataSource { //UITableViewDataSource
@IBOutlet weak var mytableview: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let realm = try! Realm()
let theItem = realm.objects(Item.self).filter("itemid >= 1")
return theItem.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let realm = try! Realm()
let theItem = realm.objects(Item.self).filter("itemid >= 1")
print(theItem)
let cell = tableView.dequeueReusableCell(withIdentifier: "cell1")
//I suspect the problem is at here...
cell?.textLabel?.text = "\(theItem)"
return cell!
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
}
class Category: Object {
@objc dynamic var name: String?
@objc dynamic var caid: Int = 0
}
class Item: Object {
@objc dynamic var name: String?
@objc dynamic var itemid: Int = 0
@objc dynamic var cateid: Int = 0
}
您的问题是您需要从 Item
对象中获取字符串。试试
"\(theItem.name)"
。
func getNames() -> [String]{
let items = realm.objects(Item.self).filter("itemid >= 1").toArray(ofType: Item.self ) as [Item]
return items.map { [=10=].name }
}
extension Results {
func toArray<T>(ofType: T.Type) -> [T] {
var array = [T]()
for i in 0 ..< count {
if let result = self[i] as? T {
array.append(result)
}
}
return array
}
}
我已经找到了显示数据的方法。我只需要在我的代码中添加 indexPath.row
它就可以处理数据了。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let realm = try! Realm()
let theItem = realm.objects(Item.self).filter("itemid >= 1")
//I only add below this indexpath
let cellData = theItem[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "cell1")
//and change this part and it's done.
cell?.textLabel?.text = cellData.name
print(theItem)
return cell!
}