In my case I have struct with data and I would like use value _host to titleForHeaderInSection in tableview and group by _host data
var polozkyuctu = [polozky]()
struct polozky: Codable {
let _id: Int
let _recept: String
let _host: String
}
Data in polozkyuctu:
[Cheers.Ucet.polozky(_id: 453741, _recept: "BECHEROVKA 1L", _host: "0"),
Cheers.Ucet.polozky(_id: 453718, _recept: "PALAČINKY DEZERT", _host: "10“),
Cheers.Ucet.polozky(_id: 453719, _recept: "TLAČENKA SVĚTLÁ S CIBULÍ", _host: "10“),
Cheers.Ucet.polozky(_id: 453720, _recept: "PAŠTIKA JÁTROVÁ", _host: "0"),
Cheers.Ucet.polozky(_id: 453737, _recept: "ABSINTH ŽUFÁNEK 0,5L", _host: "2")]
extension Ucet: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
let hh = Dictionary(grouping: polozkyuctu, by: { (element: polozky) in
return element._host })
print ("hostcount: \(hh.count)")
// hostcount: 3
return hh.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return ???
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return „Title \(???)“
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
???
return cell
}
How to fit the "section" correctly so that the result looks in datatable like this?
Title 0
BECHEROVKA 1L
PAŠTIKA JÁTROVÁ
Title 2
ABSINTH ŽUFÁNEK 0,5L
Title 10
PALAČINKY DEZERT
TLAČENKA SVĚTLÁ S CIBULÍ