Arrays 如何有效地将领域中所有数据行的属性字符串加载到UITableView中的UILabels中
在我的领域中,我有Arrays 如何有效地将领域中所有数据行的属性字符串加载到UITableView中的UILabels中,arrays,swift,uitableview,properties,realm,Arrays,Swift,Uitableview,Properties,Realm,在我的领域中,我有RealmClassA,其中有许多行信息,用户可以在模拟器中运行时成功添加这些信息 我正在尝试从领域(技术术语Query?!)读取数据,以将一些数据显示到TableViewController中的customCell中,其中包含许多uilabel。customCell的类型为TableViewCell 例如,我只包含了一些属性和标签 我想将propertyA列中的数据按字母顺序显示到UIlabel中,并与该行条目的propertyB列中的其他数据一起显示。见下图 TableV
RealmClassA
,其中有许多行信息,用户可以在模拟器中运行时成功添加这些信息
我正在尝试从领域(技术术语Query
?!)读取数据,以将一些数据显示到TableViewController
中的customCell
中,其中包含许多uilabel
。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)
它打印出内存中RealmClassA的数量为10
当我试图只打印propertyA列中stings的名称时
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.
查询领域LazyMapSequence(\u base:Results)中的所有propertyA条目(
[0]RealmClassA{
propertyA=填充词的随机词
propertyB=更多的随机字来填充属性信息;
//等等。
},
//这将对RealmClassA中的所有条目重复。
需要的帮助与
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
}
由于@Jay的帮助和本网站的帮助,在答案发布后删除不必要的信息以保持清晰;
, 我发现在
cellForRowAt
func的末尾缺少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-iOS上设置tableView数据源与此非常相似。这将设置一个包含10个对象的数据源。此示例旨在展示数据源的正确使用
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。Wow-这是很多信息。是否有一个特定的问题?是否可以将问题稍微编辑一下,使其更加集中?另外,
allEntriesList.map{$0.PropertyA}
将是一个问题,因为您的领域对象没有.PropertyA
,但它有.PropertyA
。此外,如何有效地访问领域中要在表视图中显示的数据,只需从tableView数据源获取该行的对象让realmObject=dataSource[row]
然后获取属性让someData=realmObject.property
然后将其分配给textFieldcell.textField.text=someData
谢谢Jay。为了清楚起见,我已经更新了我的问题,将主要问题包括在内。我具体将我的realmObject=dataSource[行]放在哪里
请?对我来说,它是让realmclasa=dataSource[row]
,让propertyAList=realmclasa.propertyA
并且在UILabel中,cell.textField.text=propertyAList
它将在表视图中(uu.tableView:cellForRowAt
函数,就在单元格实例化之后,让cell=tableView.
。这是填充单元格的一种方法-从数据源获取相关数据,然后从中填充单元格的字段、图像等。对其余的注释,这是正确的顺序,尽管不使用变量名的大写字母。很高兴您找到了解决方案。但是,这通常不是一个好的解决方案。在tableView委托函数期间,您永远不希望访问数据库。tableView的设计速度快且轻量级,它们应该使用最少量的代码来更新UI。这let itemsToDisplay=realm.ob对象(
将导致它在从磁盘提取数据时变慢,性能不佳。您应该使用该函数填充tableView数据源(通常是一个数组)并从该数组(在内存中,而不是磁盘中)提取数据当填充tableView单元格时。谢谢Jay。我已经从cellForRowAt override func中删除了itemsToDisplay…
,现在使用let-itemsToDisplay=Array(realm.objects(RealmClassA.self)。排序(byKeyPath:“propertyA”)
下面导入RealmSwift
获取错误:使用未解析的标识符“realm”。当放置在类TableViewController:…
下面时,我获取错误:无法在属性初始值设定项中使用实例成员“realm”;属性初始值设定项在“self”可用之前运行。当放入viewDidLoad方法时,cell.propertyAdisplayLabel?.text etc
对于所有属性,当涉及Swift和Xcode时,错误“使用未解析标识符”itemToDisplay仍然很小,知道将代码放在哪里通常是我的学习曲线。我假设我使用了不正确的方法创建数组,或者我尝试创建Ar