0
I have a message tableview, I would like to know how to add sections using the date field of the Notificationitem object.
struct NotificationItem: Codable {
let title: String
let body: String
let date: Date
let link: String
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if notificationStore.items.count == 0 {
self.viewSemNotificacoes.isHidden = false
} else {
self.viewSemNotificacoes.isHidden = true
}
return notificationStore.items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
if let newsCell = cell as? NotificationItemTableViewCell {
newsCell.updateWithNewsItem(notificationStore.items[indexPath.row])
}
return cell
}
I wonder if it is possible to add Sectionheader using the date field even without this object having an array of sections. I’ve already inserted sections in tableviews but always using the array inside the object.
Exactly need that in Action have n messages, then in this case your example will not meet but the logic was good.
– Julio Figueiredo
Then you’ll need a multidimensional structure. When the view controller is created you can traverse the current data structure and assemble a new one as required by that screen. A simple solution is to use tuples, e.g. var Notifications = (Date, [Notificationitem]), Notifications[Section]. 0 to access a certain section and Notifications[Section]. 1[Row] accesses the Notificationitem object to mount each cell
– Rafael Leão