1

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

Anh Pham
  • 2,108
  • 9
  • 18
  • 29
Marco
  • 65
  • 11
  • `headerCell.headerLabel.text = MenuViewController.tasksArray[section].date` – Larme Jul 10 '17 at 13:38
  • Set UITableView style as Grouped, not plain. https://stackoverflow.com/a/17583567/7084910 – Jay Patel Jul 10 '17 at 13:38
  • now the labels with different dates are ok! There is still the problem that the task is always the same, like in the photo instead of being different – Marco Jul 10 '17 at 16:36

1 Answers1

2

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.

Fangming
  • 24,551
  • 6
  • 100
  • 90
  • There isn't indexPath in that function, but I have corrected the code with the answer below. Thank you – Marco Jul 10 '17 at 16:53
  • @Marco Hey sorry, I thought that is `cellForRow` method. For your section header method, user section instead of indexPath. I just updated my answer – Fangming Jul 10 '17 at 16:55
  • Thanks a lot! Can you answer to this question: now the dates are different and it's what I want, but the tasks are always "Incontro", like in the picture, instead of being different like in the array that I have written in the question – Marco Jul 10 '17 at 17:53
  • I think that is because in every section is displayed and loaded every time the first element. Can you help me? – Marco Jul 10 '17 at 17:54
  • @Marco Yes! Look at my answer carefully. You are getting the first element by calling [0] all the times, which is the first element in your array. Change that to section variable which is an index telling you which section the current one is – Fangming Jul 10 '17 at 17:58
  • So I have to move the code that I have written in cellForRowAt in the function viewForHeaderInSection? – Marco Jul 10 '17 at 18:05
  • @Marco No... I have two lines of code in my answer, right? Search for the 1st line in your code, and replace that line with my 2nd line of code – Fangming Jul 10 '17 at 18:06
  • There is another problem, if you look at the picture you see that there is always "Incontro" instead of "Incontro", "Compito", "Incontro" and "Interrogazione". And that cell is configured in the cellForRowAt in my code – Marco Jul 10 '17 at 18:20
  • @Marco I see that in your `cellForRow`, you are getting elements by `tasksArray[indexPath.row]`, which is correct. If in this case you are still getting same information, print that tasksArray out and see if it contains duplicated label itself. – Fangming Jul 10 '17 at 18:24
  • The array doesn't contain duplicated label, it seems like in every new section reload the indexPath and so takes every time the first element – Marco Jul 10 '17 at 22:05
  • @Marco Updated my answer. Check it out now ! – Fangming Jul 10 '17 at 22:31