Sectionheader tableview Swift

Asked

Viewed 58 times

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.

1 answer

1

If you have not implemented the numberOfSectionsInTableView:method, tableView by default has a section and 'n' cells, where 'n' is the number of elements in the array 'notificationStore.items'. Using this same data structure, you could create a tableView with 'n' sections, each with a cell:

func numberOfSections(in tableView: UITableView) -> Int {
    return notificationStore.items.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
    return dateFormatter.string(from: notificationStore.items[section].date)
}

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.section])
    }

    return cell
}

If you need multiple sections and also show multiple messages in the same section, the one-dimensional array does not meet your needs. In this case you would need a multimensional structure, for example a set where the key would be the date and the value would be an array with the elements of that section.

  • Exactly need that in Action have n messages, then in this case your example will not meet but the logic was good.

  • 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.