如何有效地将领域中所有数据行的属性字符串加载到UITableView中的UILabel中 [英] How to effectively load the properties string for all rows of data from realm into UILabels in a UITableView

查看:55
本文介绍了如何有效地将领域中所有数据行的属性字符串加载到UITableView中的UILabel中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的范围内,我有RealmClassA,其中包含许多行的信息,用户可以在模拟器中运行时成功添加到这些行中.

Within my realm I have RealmClassA, with many rows of information that the user can successfully add to whilst running in simulator.

我正在尝试从领域(技术术语Query ?!)中读取数据,以将某些数据显示在容纳许多UILabelscustomCell内的TableViewController中. customCell的类型为TableViewCell.

I am trying to read the data from realm (technical term Query?!) to display some of the data into a TableViewController within a customCell that houses many UILabels. The customCell is of type TableViewCell.

为方便起见,我仅包含了一些属性和标签.

For examples sake I have included only some of the properties and labels.

我想按字母顺序将propertyA列中的数据显示到UIlabel中,并将该行条目的其他数据显示在propertyB列中.参见下图.

I would like to display the data from propertyA column, into the UIlabel's in alphabetical order, alongside the other data in propertyB column for that row entry. See image below.

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
    }

}

我可以使用以下方法计算输出中的条目数;

I am able to count the number of entries in the Output using the following;

print("The number of RealmClassA records in memory is",realm.objects(RealmClassA.self).count)

它打印出The number of RealmClassA in memory is 10

当我尝试仅打印propertyA列中的st的名称时;

When I attempt to print just the names of the stings in propertyA column;

    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. 

需要的帮助

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

  1. HOW to effectively access the data from realm to display in the table view. I'm clearly nearly there, as I can display the correct number of cells for the number of entries in RealmClassA.

我是否已经解决了一些复杂的问题.

Whether I have over complicated something along the line.

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

I have used the following sources as help to no avail.

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

具有使用Realm和Swift的多节的UITableView

查询领域以使用多个节填充numberOfRowsInSection和cellForRowAt领域和Swift

如何访问Realm Swift中从主键查询返回的对象的属性

我已经在这里搜索了Realm.io文档

And I have scoured the Realm.io documentation here

https://academy.realm.io/posts/realm-list-new-superpowers-array-primitives/

https://realm.io/docs/swift/latest/#getting -开始在列表"部分

https://realm.io/docs/swift/latest/api/Classes/List.html

更新

始终将"PropertyA"的所有实例更改为"propertyA".

Changing any instances of ‘PropertyA’ to ‘propertyA’ throughout.

主要问题,以求清楚. 如何在表格视图单元格的UILabels中按字母顺序显示领域中保存的一列中的所有数据.

Main question for clarity. How to display all data from one column in saved in realm, in an alphabetical order inside a tableview cells’ UILabels.

很抱歉在帖子的长度上,我看到很多人在询问所有信息,并对此发表了评论,所以我已经竭尽全力了!

Apologies for the length of post, I see a lot of questions commented with people asking for all the information so I have done my utmost!

更新2

发布答案后,为了清晰起见,删除不必要的信息

Removing unnecessary information for clarity after answer posted

推荐答案

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

Thanks to @Jay's help plus this website;
https://www.ralfebert.de/ios-examples/uikit/uitableviewcontroller/custom-cells/, I found I was missing as! customCell from the end of the cellForRowAt func.

如果有人正在进一步研究此内容,则应该是这样.

Here is how it should look if anyone is looking at this further down the line.

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.这个示例确实是为了展示对dataSource的正确使用.

Setting up a tableView dataSource on macOS - iOS is very similar. This sets up a dataSource with 10 objects. This example is really to show a proper use of the 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.

Note the NSTableCellView (Table Cell View) identifier is set to MyCellView.

这篇关于如何有效地将领域中所有数据行的属性字符串加载到UITableView中的UILabel中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆