Marco
Marco

Reputation: 57

Header of cells in UITableView

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:

enter image description here

Upvotes: 1

Views: 1143

Answers (1)

Fangming
Fangming

Reputation: 25261

First, you are always displaying the first task because you are always asking for the first one by this code

headerCell.headerLabel.text = MenuViewController.tasksArray[0].date

To make it right, you need to get task base on the current index of your cell, which means

headerCell.headerLabel.text = MenuViewController.tasksArray[section].date

Second, to make header follows your cell, you need to set the table view style to be grouped, like this

enter image description here

For your labels, change the following code

cell.textLabel?.text = MenuViewController.tasksArray[indexPath.row].subject
cell.detailTextLabel?.text = MenuViewController.tasksArray[indexPath.row].comment

Into

cell.textLabel?.text = MenuViewController.tasksArray[indexPath.section].subject
cell.detailTextLabel?.text = MenuViewController.tasksArray[indexPath.section].comment

You are having one cell per section. So the row in each section is always 0. That's why you are getting the same label over and over again.

Upvotes: 2

Related Questions