I have an array of tasks like this:
68 - Incontro - Incontro - 10/07/2017 - Incontro robot
69 - Compito - Matematica - 11/07/2017 - Pag 620 n.19
71 - Incontro - Incontro - 11/07/2017 -
70 - Interrogazione - Matematica - 12/07/2017 - da pag 200 a pag 230
Where the first parameter is an ID, the second is the type, the third is the subject, the fourth is the date and the last is the comment.
I want a table view that displays all the elements of my array each one in a cell with the name and the comment and I want that all cells have a header with the date. My class is this:
class TasksViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
func numberOfSections(in tableView: UITableView) -> Int {
return MenuViewController.tasksArray.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 45
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 10
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCell(withIdentifier: "headerCell") as! CustomHeaderCellTableViewCell
headerCell.backgroundColor = UIColor.cyan
headerCell.headerLabel.text = MenuViewController.tasksArray[0].date
return headerCell
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
tableView.rowHeight = 70
cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
cell.backgroundColor = funzioni.hexStringToUIColor(hex: "#e5e5ec")
cell.textLabel?.text = MenuViewController.tasksArray[indexPath.row].subject
cell.detailTextLabel?.text = MenuViewController.tasksArray[indexPath.row].comment
return cell
}
}
My problem is that is displayed every time the first task. And I also want not to move the header when I go down in the tableView, like in the picture: