如何有效地将领域中所有数据行的属性字符串加载到 UITableView 中的 UILabels

How to effectively load the properties string for all rows of data from realm into UILabels in a UITableView

在我的领域内,我有 RealmClassA,用户可以在模拟器中 运行 中成功添加多行信息。

我正在尝试从领域(技术术语 Query?!)读取数据,以将一些数据显示到 customCell 中的 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{[=13=].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. 

需要帮助
  1. 如何有效地访问领域中的数据以显示在 table 视图中。我显然快到了,因为我可以为 RealmClassA 中的条目数显示正确的单元格数。

  2. 我是不是把事情搞得太复杂了。

我使用了以下资源作为帮助但无济于事。

https://www.youtube.com/watch?v=0WOd6GVPMb0

我在这里搜索了 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”。

为清楚起见,主要问题。 如何在 table 视图单元格的 UILabel 中按字母顺序显示领域中保存的一列中的所有数据。

抱歉 post 的篇幅,我看到很多评论的问题都要求提供所有信息,所以我已经尽力了!

更新 2

为清楚起见,在回答后删除不必要的信息 posted

感谢@Jay的帮助加上本站;
https://www.ralfebert.de/ios-examples/uikit/uitableviewcontroller/custom-cells/, 我发现 cellForRowAt 函数的末尾缺少 as! customCell

如果有人正在进一步查看它,它应该是这样的。

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
    }

编辑

在 macOS 上设置 tableView 数据源 - iOS 非常相似。这将设置一个包含 10 个对象的数据源。这个例子是为了展示如何正确使用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(Table 单元格视图)标识符设置为 MyCellView。