Need help displaying Dictionary into Tableview

Hey guys, I’m designing an app that analyzes data from the accelerometer from the phone.
What I’m trying to do is display that data collected into a a tableview. The basic outline is that it sampling data every 0.2sec and should display the data into sections (each section is a second) with 5 rows (each row is the sampled data every 0.2sec)

What I am trying to display is that each section is labeled the appropriate second and in each section showing the accelerometer data (Z axis):

Each row is the accelerometer data collected a every 0.2sec (hence each section is 0.2sec * 5)

What I thought would work is take the data and store it into an array. I’m having trouble breaking up the data for display.

I then created a dictionary or arrays where each key is the “second” and the values are an array of the accelerometer data for that second. My thinking was that I could label each section based on the key and display the values for that key into the rows but I’m stuck and not sure if that was the right approach.

I tried storing the keys and values into arrays in my tableview controller using keys = acceldata.keys but can extract the values as individual elements of that array.


acceldata[1] : [0.2, 0.4, 0.6, 0.8, 1.0]

acceldata[2] : [-0.2, -0.4, -0.6, 0.8, -1.0]

How I would like for it to be displayed:

second 1

  • 0.2
  • 0.4
  • 0.6
  • 0.9
  • 1.0

second 2

  • -0.2
  • -0.4
  • -0.6
  • -0.8
  • -1.0

I am not sure if this logic was correct or if there is another way of doing this. Any advice would be helpful. Should I stick with the dictionary method? My hope was that I could use each key as the title for the section and value for that key as the data source for the rows.

I figured it out…

Please post your solution!!!
It may help someone else

To display the values from the dictionary:

In the cellForRow method to display the individual cell

let cellDisplay = acceldata[indexPath.section]?[indexPath.row]
cell.cellDisplay?.text = "\(cellDisplay)"
return cell

This translates as:

access the first section then each element in the array in the value of that key

so acceldata[1] = [[0.2, 0.4, 0.6, 0.8, 1.0] would translate as the following to display:

indexPath.section = 1
indexPath.row = 0 would be 0.2, 1 = 0.4 and so on.

To display the keys - it was a little more involved.

First I had to store the value of the keys into an array into a separate function

arrayOfNumberOfKeys = Array(acceldata.keys)

This stored the keys into an array so I could access each element to display as the section header.

In the function titleForHeaderInSection - I called the above method and returned the value into a string

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return String("Second \(arrayOfNumberOfKeys[section]+1)")

I added the +1 one because the first section is to be data for the first second.

Hope that is helpful!