0

I have an array of 12 articles (0 through 11). These 12 articles are divided into sections based on 3 topics, with each topic name becoming the section header title. My problem is that the index path counter resets to 0 at the start of each section, so I get this:

***Topic 0
article 0
article 1
article 2
***Topic 1
article 0
article 1
article 2
***Topic 2
article 0
article 1
article 2
article 3
article 4
article 5

instead of what I want, which is this:

***Topic 0
article 0
article 1
article 2
***Topic 1
article 3
article 4
article 5
***Topic 2
article 6
article 7
article 8
article 9
article 10
article 11

What is incorrect about my implementation?

Kevin Kelly
  • 164
  • 1
  • 12

2 Answers2

3

Your data structure isn't setup correctly. You want an array of section data. Each element of the array should be a dictionary. Each dictionary should have a key for the section title and a key for an array of rows in that section.

Having one big array isn't appropriate for a sectioned table.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
0

In your cellForRowAtIndexPath method you need to offset the array element you're retrieving by the offset of the section. I don't know how you have your sections configured but each section has an offset. The first section's offset is 0. The second offset is the number of rows in the first section, etc. Use that index to retrieve the row in your array that corresponds to the right section.

Richard Brown
  • 11,346
  • 4
  • 32
  • 43