结构 v Class - TableView 隐藏数据
Struct v Class - TableView Hidden Data
我的代码 运行 遇到了问题,我是新手,所以我边学边学(所以请宽容!)。本质上,我有一个 tableView,它在用户添加数据时显示数据。例如,用户在多日内增加了体重,结果显示出来了。我发现如果我为 'Log' 使用一个结构,它只会显示一个包含一份数据的表格视图单元格。但是,当我对 'Log' 使用 class 时,它会显示所有数据。一般来说是这样吗?我最初将 tableview 的代码编写为 'class Log' 但需要将其更改为结构,我遇到了这个问题。我希望代码易于阅读!有人可以让我知道如何改进我的代码/给我任何指示吗?谢谢!
VC
struct Log {
var title: String
var Diary: [Diary]
init(title: String, Diary: [Diary]) {
self.title = title
self.Diary = Diary
}
}
struct Diary {
var id: String?
var Weight: String
var Body: String
var date: String?
var dateFormatted: Date?
init(id: String? = nil, Weight: String, Body: String, date: String? = nil, dateFormatted: Date? = nil) {
self.id = id
self.Weight = Weight
self.Body = Body
self.date = date
self.dateFormatted = dateFormatted
}
}
@IBOutlet weak var logList: UITableView!
@IBOutlet weak var logTitle: UILabel!
var Diary = [Diary]()
var Logs: [Log] = [Log].init()
override func viewDidLoad() {
super.viewDidLoad()
self.fetchData(for: todayDate)
}
获取数据
func fetchData(for date: String) {
guard let userId = Auth.auth().currentUser?.uid else { return }
let LogRef = Database.database().reference().child("users/\(userId)/Log")
LogRef.removeAllObservers()
databaseRef = LogRef.child(date)
databaseRef.observe(.value, with: {(snapshot) in
self.Diary.removeAll()
self.Logs.removeAll()
if snapshot.childrenCount>0 {
for title in snapshot.children.allObjects as! [DataSnapshot] {
print("title = ", title)
if let logObject = title.value as? [String: AnyObject],
let title = logObject["Body"] as? String,
let Weight = logObject["Weight"] as? String {
let title = Diary(id: title.key, Body: Body, Weight: Weight)
self.Diary.append(title)
if var Log = self.Logs.first(where: {[=12=].title == title}) {
Log.Diary.append(title)
}else {
self.Logs.append(Log.init(title: title, Diary: [title]))
}
}else if title.key == "logTitle",
let logTitle = title.value as? String {
self.logTitle.text = logTitle
}
}
}
self.logList.reloadData()
})
}
相关的 TABLEVIEW 代码:
func numberOfSections(in tableView: UITableView) -> Int {
return self.Logs.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return self.Logs[section].title.uppercased()
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if self.hiddenSections.contains(section) {
return 0
}
return self.Logs[section].Diary.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = logList.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! LogTableViewCell
let title: Diary = self.Logs[indexPath.section].Diary[indexPath.row]
cell.body.text = title.Body
cell.weight.text = title.Weight
return cell
}
}
主要归结为这一行:
if var Log = self.Logs.first(where: {[=10=].title == title}) {
Log.Diary.append(title)
}
如果一个日志是一个 class 实例,这会改变数组 中日志的日记数组 self.Logs
。
但是如果 Log 是一个结构实例,它就不是。 var Log
是一个副本,更改它对 self.Logs
.
中的任何内容都没有影响
有关解决方法,请参阅我的 。正如我所说:
When you have an array of struct, then in order to make a change to a struct within the array, you must refer to that struct by index.
所以
if let index = self.Logs.firstIndex(where: {[=11=].title == title}) {
self.Logs[index].Diary.append(title)
}
我的代码 运行 遇到了问题,我是新手,所以我边学边学(所以请宽容!)。本质上,我有一个 tableView,它在用户添加数据时显示数据。例如,用户在多日内增加了体重,结果显示出来了。我发现如果我为 'Log' 使用一个结构,它只会显示一个包含一份数据的表格视图单元格。但是,当我对 'Log' 使用 class 时,它会显示所有数据。一般来说是这样吗?我最初将 tableview 的代码编写为 'class Log' 但需要将其更改为结构,我遇到了这个问题。我希望代码易于阅读!有人可以让我知道如何改进我的代码/给我任何指示吗?谢谢!
VC
struct Log {
var title: String
var Diary: [Diary]
init(title: String, Diary: [Diary]) {
self.title = title
self.Diary = Diary
}
}
struct Diary {
var id: String?
var Weight: String
var Body: String
var date: String?
var dateFormatted: Date?
init(id: String? = nil, Weight: String, Body: String, date: String? = nil, dateFormatted: Date? = nil) {
self.id = id
self.Weight = Weight
self.Body = Body
self.date = date
self.dateFormatted = dateFormatted
}
}
@IBOutlet weak var logList: UITableView!
@IBOutlet weak var logTitle: UILabel!
var Diary = [Diary]()
var Logs: [Log] = [Log].init()
override func viewDidLoad() {
super.viewDidLoad()
self.fetchData(for: todayDate)
}
获取数据
func fetchData(for date: String) {
guard let userId = Auth.auth().currentUser?.uid else { return }
let LogRef = Database.database().reference().child("users/\(userId)/Log")
LogRef.removeAllObservers()
databaseRef = LogRef.child(date)
databaseRef.observe(.value, with: {(snapshot) in
self.Diary.removeAll()
self.Logs.removeAll()
if snapshot.childrenCount>0 {
for title in snapshot.children.allObjects as! [DataSnapshot] {
print("title = ", title)
if let logObject = title.value as? [String: AnyObject],
let title = logObject["Body"] as? String,
let Weight = logObject["Weight"] as? String {
let title = Diary(id: title.key, Body: Body, Weight: Weight)
self.Diary.append(title)
if var Log = self.Logs.first(where: {[=12=].title == title}) {
Log.Diary.append(title)
}else {
self.Logs.append(Log.init(title: title, Diary: [title]))
}
}else if title.key == "logTitle",
let logTitle = title.value as? String {
self.logTitle.text = logTitle
}
}
}
self.logList.reloadData()
})
}
相关的 TABLEVIEW 代码:
func numberOfSections(in tableView: UITableView) -> Int {
return self.Logs.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return self.Logs[section].title.uppercased()
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if self.hiddenSections.contains(section) {
return 0
}
return self.Logs[section].Diary.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = logList.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! LogTableViewCell
let title: Diary = self.Logs[indexPath.section].Diary[indexPath.row]
cell.body.text = title.Body
cell.weight.text = title.Weight
return cell
}
}
主要归结为这一行:
if var Log = self.Logs.first(where: {[=10=].title == title}) {
Log.Diary.append(title)
}
如果一个日志是一个 class 实例,这会改变数组 中日志的日记数组 self.Logs
。
但是如果 Log 是一个结构实例,它就不是。 var Log
是一个副本,更改它对 self.Logs
.
有关解决方法,请参阅我的
When you have an array of struct, then in order to make a change to a struct within the array, you must refer to that struct by index.
所以
if let index = self.Logs.firstIndex(where: {[=11=].title == title}) {
self.Logs[index].Diary.append(title)
}