在我的领域中,我有RealmClassA
,在模拟器中运行时,用户可以成功地添加许多行信息。
我正在尝试从领域读取数据(技术术语Query
?!)将一些数据显示在包含多个TableViewController
的customCell
中的UILabels
中。customCell是TableViewCell
类型的。
例如,我只包含了一些属性和标签。
我希望将propertyA
列中的数据按字母顺序显示到UIlabel中,并为该行条目在propertyB
列中显示其他数据。见下图。
TableViewController
是;
import UIKit
import RealmSwift
class TableViewController: UITableViewController {
let realm = try! Realm()
override func viewDidLoad() {
super.viewDidLoad()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
//After solving this question, this numberOfSections return will return the count of the number of unique strings that the propertyB column that RealmClassA houses.
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
return realm.objects(RealmClassA.self).count
// this DOES return a number of cells that matches the number of entries (rows) in RealmClassA.
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "reusableCellID", for: indexPath)
// this is where I have hit a brick wall and guidance is needed.
return cell
}
}
customCell
为TableViewCell
型;
import UIKit
import RealmSwift
class TableViewCell: UITableViewCell {
@IBOutlet weak var propertyAGoesHere: UILabel!
@IBOutlet weak var propertyBGoesHere: UILabel!
@IBOutlet weak var propertyCGoesHere: UILabel!
//etc.
let realm = try! Realm()
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
我可以使用以下方法来计算输出中的条目数;
print("The number of RealmClassA records in memory is",realm.objects(RealmClassA.self).count)
它打印出The number of RealmClassA in memory is 10
当我试图在propertyA列中只打印刺的名称时;
let allEntriesArray = allEntriesList.map{$0.PropertyA}
print("Querying all propertyA entries in realm \(allEntriesArray)")
印出来
Querying all propertyA entries in realm LazyMapSequence<Results<RealmClassA>, String>(_base: Results<RealmClassA> <0x7fd2f8cb4f20> (
[0] RealmClassA {
propertyA = Random Words for Filler
propertyB = More Random words to fill the property information;
//etc.
},
// this repeats for all entries in the RealmClassA.
需要的帮助
中的条目数显示正确的单元格数
我使用了以下资料来帮助,但没有用。
https://www.youtube.com/watch?v=0WOd6GVPMb0
UITableView with Multiple Sections using Realm and Swift
How to access properties of an object returned from primary key query in Realm Swift
我已经在这里浏览了Realm.io文档
https://academy.realm.io/posts/realm-list-new-superpowers-array-primitives/
列表部分中的https://realm.io/docs/swift/latest/#getting-started
https://realm.io/docs/swift/latest/api/Classes/List.html
更新
将“propertyA”的所有实例更改为“propertyA”。
主要问题是要清楚。如何显示保存在领域中的某一列的所有数据,在表视图单元格的UILabels中按字母顺序显示。
对于帖子的长度,我很抱歉,我看到了很多问题,人们问我所有的信息,所以我已经尽了我最大的努力!
更新2
删除不必要的信息,以便在发帖后澄清。
发布于 2020-06-30 05:31:38
感谢@Jay的帮助加上这个网站;
https://www.ralfebert.de/ios-examples/uikit/uitableviewcontroller/custom-cells/,我发现我错过了as! customCell
的cellForRowAt
功能。
如果有人进一步关注这个问题,我们应该如何看待这个问题。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "reusableCellID", for: indexPath) as! customCell
let itemsToDisplay = realm.objects(RealmClassA.self).sorted(byKeyPath: "propertyA")[indexPath.row]
cell.propertyADisaplyLabel?.text = itemsToDisplay.propertyA
cell.propertyBDisplayLabel?.text = itemsToDisplay.propertyB
cell.propertyCDisplayLabel?.text = itemsToDisplay.propertyC
return cell
}
编辑
在tableView - iOS上设置一个macOS dataSource非常类似。这将设置一个包含10个对象的dataSource。这个例子实际上是为了展示dataSource的正确使用。
class ViewController: NSViewController, NSTableViewDelegate, NSTableViewDataSource {
var dataArray = [String]()
@IBOutlet weak var myTableView: NSTableView!
override func viewDidLoad() {
super.viewDidLoad()
for i in 0...9 {
let s = "row_\(i)"
self.dataArray.append(s)
}
self.myTableView.delegate = self
self.myTableView.dataSource = self
self.myTableView.reloadData()
}
func numberOfRows(in tableView: NSTableView) -> Int {
return self.dataArray.count
}
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
let identifier = NSUserInterfaceItemIdentifier("MyCellView")
guard let cell = tableView.makeView(withIdentifier: identifier, owner: self) as? NSTableCellView else {return nil}
cell.textField?.stringValue = self.dataArray[row]
}
注意,NSTableCellView (表单元格视图)标识符设置为MyCellView。
https://stackoverflow.com/questions/62644039
复制相似问题