I have run into a problem in my code and I am new so I am learning as I go (so please be lenient!). Essentially, I have a tableView that shows data when the user adds them. For example, a user adds his weight on multiple days and it shows. I found out that if I use a struct for 'Log', it only shows one tableview cell with one piece of data. HOWEVER, when I use a class for 'Log', it shows all the data. Is this the case in general? I wrote the code for the tableview initially as a 'class Log' but need to change it to a struct and I am encountering this problem. I hope the code is easy to read! Could someone let me know how I could improve my code / give me any pointers? Thank you!
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)
}
GETTING DATA
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: {$0.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 CODE RELATED:
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
}
}
CodePudding user response:
It largely comes down to this line:
if var Log = self.Logs.first(where: {$0.title == title}) {
Log.Diary.append(title)
}
If a Log is a class instance, that changes the Diary array of a Log in the array self.Logs
.
But if a Log is a struct instance, it doesn't. var Log
is a copy, and changing it has no effect on anything in self.Logs
.
For the workaround see my https://stackoverflow.com/a/56916573/341994. As I say there:
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.
So
if let index = self.Logs.firstIndex(where: {$0.title == title}) {
self.Logs[index].Diary.append(title)
}