如何在表视图中获得正确数量的节.Swift,realm

  • 本文关键字:Swift realm 视图 ios swift realm
  • 更新时间 :
  • 英文 :


我有一个属性"天";在领域数据库中,当用户进行一些事务时,我会在这里保存所有的日子。这正是我需要计算的天数,以获得正确的节数。现在看起来每个事务都有一个部分,我希望有一个包含一天所有事务的部分。我的领域模型:

class Items: Object {
@objc dynamic var Amount = 0.0
@objc dynamic var category = ""
@objc dynamic var type = Bool()
@objc dynamic var month = Int()
@objc dynamic var day = Int()

}

typealias Sections = [(quantity: [Int], name: String, items: [Items])]
func getAllItems() -> [Items] {
var array: [Items] = []
let objects = realm.objects(Items.self)
for object in objects {
array.append(object)
}
return array
}
func getSections() -> Sections {
var sections: Sections = []
let data = getAllItems()
var temp = [Items]()
var summ: [Int] = []

for item in data {
for item2 in data {
if item2.day == item.day {
summ.append(item.day)
temp.append(item)
}
}
sections.append((quantity: summ, name: "(item.day)", items: temp))
temp = [Items]()
}
return sections
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
RealmModel.shared.getSections()[section].name
}
func numberOfSections(in tableView: UITableView) -> Int {
RealmModel.shared.getSections().count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return RealmModel.shared.getSections()[section].name.count
}

事实上,除了";节数";。我得到的是这样的:部分数量不正确,我需要一天一部分。我不明白我怎么能只得到";summ";从numberOfSections((中的getSection((,或者我如何通过其他方式获得正确的数量。

您的for循环可以像您想要的那样工作,因为您可以继续迭代具有与以前相同日期属性的项。并且您的numberOfRowsInSection方法无法正常工作,因为您依赖节的name属性而不是items属性。虽然我没有关于你的模型的所有信息,但我会这样做:(就像在另一个响应中说的那样,最好保存部分和项目,而不是在每次调用表视图数据源和委托时获得数据(

typealias Sections = [(quantity: [Int], name: String, items: [Items])]
func getAllItems() -> [Int: [Items]] {
var array: [Int: [Items]] = [:]
let objects = realm.objects(Items.self)
for object in objects {
if Array(array.keys).contains(object.day) {
var items = array[object.day]
items.append(object)
array[object.day] = items
} else {
array[object.day] = [object]
}
}
return array
}
func getSections() -> Sections {
var sections: Sections = []
let data = getAllItems()
for (key, value) in data {
sections.append((quantity: value.count, name: "(key)", items: value))
}

return sections
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
RealmModel.shared.getSections()[section].name
}
func numberOfSections(in tableView: UITableView) -> Int {
RealmModel.shared.getSections().count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return RealmModel.shared.getSections()[section].items.count
}

尝试带有completionHandler的解决方案:

typealias Sections = [(quantity: [Int], name: String, items: [Items])]
func getAllItems() -> [Items] {
var array: [Items] = []
let objects = realm.objects(Items.self)
for object in objects {
array.append(object)
}
return array
}
func getSections(completionHandler completion: ((_ quantity: Int) -> Void)?) -> Sections {
var sections: Sections = []
let data = getAllItems()
var temp = [Items]()
var summ: [Int] = []

for item in data {
for item2 in data {
if item2.day == item.day {
summ.append(item.day)
temp.append(item)
}
}
sections.append((quantity: summ, name: "(item.day)", items: temp))
temp = [Items]()
}
if let tCompletion = completion {
tCompletion(temp.count)
}
return sections
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
RealmModel.shared.getSections()[section].name
}
func numberOfSections(in tableView: UITableView) -> Int {
getSections() { quantity in
return quantity
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return RealmModel.shared.getSections()[section].name.count
}

尝试只计算一次节,并将其保存到数组栏中,以便在表视图数据源和委托中使用。

我认为目标是获取一个领域对象数组,确定这些对象的唯一天数,并将它们用作tableView中的节头。所以tableView看起来有点像

Day 1
item
item
item
Day 2
item
item
item

如果是这样的话,下面是简单的方法。假设你已经将Realm中的项目加载到一个数组中,所以它看起来像这个

let itemArray = [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9]

然后添加一个单独的数组用作节头,称为排序日数组

let dayArray = itemArray.map { $0.day } //all of the day properties
let daySet = Set(dayArray) //a unique set of day properties
let sortedDayArray = Array( daySet).sorted() //cast the set to a sorted array

因此sortedDayArray将是唯一排序的日期编号。从那里获得的节数

func numberOfSections(in tableView: UITableView) -> Int {
sortedDayArray.count
}

并获得任何特定部分的标题

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let dayNum = sortedDayArray[section]
let title = "Day (dayNum)"
return title
}

然后得到一个区段中的行数

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return itemArray.filter { $0.day == section }.count
}