A client wanted his app to show a list of contacts. He wanted each contacts whose name began with the same letter to be in the same section of a table view. However, he was not satisfied with the first version I coded, because empty sections would show. For instance, if there was no contact whose name began with a B, there would be an empty B section.
So, I went back to my code, and I came up with this result:
My code only handles letters from A to Z, with no diacritics. I was not aware of the UILocalizedIndexedCollation class at the time I programmed the first version, and when I discovered it, it seemed to present an interesting feature: it can handle other locales, in particular, Arabic or Asian alphabets.
For the second version, I thought it would be better using it, but I really wasted two hours:
UILocalizedIndexedCollation uses a fixed list of indexes, so empty sessions are shown. Just like the first version I came up with. I thought Apple would have had produced a much better implementation.
By the way, Apple’s documentation is way too spare. NSHipster provides us with a good starting point to understand how to use this class.