Search code examples
uitableviewxamarin.iossections

Multiple Sections in one SectionIndexTitles


I have now a UITableView with multiple sections in it. Only if I add a list with the start letters of the sections title (ABCDE etc) as SectionIndexTitles and the list is not yet filtered for double values.

So the list would be:

  • A
    • Header = ABBA
    • CD 1
    • CD 2
  • A
    • Header = AC/DC
    • CD 1
    • CD 2
  • B
    • Header = Backstreet Boys
    • CD 1
    • CD 2
  • D
    • Header = D12
    • CD 1
    • CD 2
  • D
    • Header = Damien Rice
    • CD 1
    • CD 2
  • D
    • Header = David Guetta
    • CD 1
    • CD 2
  • E
    • Header = Elvis Presley
    • CD 1
    • CD 2
  • E
    • Header = Eric Clapton
    • CD 1
    • CD 2

Everything works fine but what I want is the following:

  • A
    • Header = ABBA
    • CD 1
    • CD 2
    • Header = AC/DC
    • CD 1
    • CD 2
  • B
    • Header = Backstreet Boys
    • CD 1
    • CD 2
  • D
    • Header = D12
    • CD 1
    • CD 2
    • Header = Damien Rice
    • CD 1
    • CD 2
    • Header = David Guetta
    • CD 1
    • CD 2
  • E
    • Header = Elvis Presley
    • CD 1
    • CD 2
    • Header = Eric Clapton
    • CD 1
    • CD 2

And if you go to the letter E, you start with Elvis Presley.

Getting all the first letters available is not a problem, but if I run the application and click on the E, the list goes to D12 instead of Elivs Presley.

So the problem is something to do with the index of the SectionIndexTitles and the sections self but I can't find a solution...


Solution

  • Try to distinct your array before it will be used in SectionIndexTitles:

    sectionTitles = sectionTitles.Distinct().ToList();
    

    Thus, you will remove doubled A and other letters in section titles.

    Note that you should to check SectionFor method code to correctly scroll UITableView to your sections.