Solucionado (ver solução)
Solucionado
(ver solução)
3
respostas

Section tableview

Tenho uma Tableview de mensagens, gostaria de saber como faço para adicionar sections usando o campo date do objeto NotificationItem.

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
    }
3 respostas

Seguindo esse blog aqui:

let section = ["pizza", "deep dish pizza", "calzone"]
let items = [["Margarita", "BBQ Chicken", "Pepperoni"], ["sausage", "meat lovers", "veggie lovers"], ["sausage", "chicken pesto", "prawns", "mushrooms"]]

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return self.section\[section\]
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
     // #warning Incomplete implementation, return the number of sections

    return self.section.count

}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
 // #warning Incomplete implementation, return the number of rows

    return self.items\[section\].count

}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
 let cell = tableView.dequeueReusableCellWithIdentifier("tableCell", forIndexPath: indexPath)

// Configure the cell...

cell.textLabel?.text = self.items[indexPath.section][indexPath.row]

return cell

}

Bom dia Diego,

Com essa estrutura que tem ai no blog ok é tranquilo de implementar, a minha duvida é se com a estrutura que tenho da pra fazer. Tenho uma lista simples de mensagens porém gostaria de usar o campo date como section visto que posso ter varias mensagens no mesmo dia. Minha ideia seria fazer um converter na data para vir no formato "dd/MM/yyyy". Porém não sei se seria possivel e nem sei como usar este parametro como section.

solução!

Creio que você terá que filtrar a sua lista primeiro, buscando pelas datas e agrupando elas em um novo array ou dictionary, e dando push no array.

Algo parecido com isso

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software