當 UITableView 顯示大量資料,例如時區、城市或聯絡人,單靠垂直捲動會讓使用者難以快速定位。UILocalizedIndexedCollation 是 iOS 提供的索引工具,能依當地語系規則把資料分組並排序,再搭配右側索引列使用。
範例以時區為主題,建立 ZYTimeZoneWrapper 物件保存 localeName。在 configureSections 中,先取得 UILocalizedIndexedCollation 的 currentCollation,再用 sectionTitles 建立對應的 section 陣列。接著透過 sectionForObject:collationStringSelector: 判斷每筆資料屬於哪一區,最後用 sortedArrayFromArray:collationStringSelector: 排序。
numberOfSectionsInTableView: 回傳 section 數量;titleForHeaderInSection: 回傳區段標題;sectionIndexTitlesForTableView: 提供右側索引;sectionForSectionIndexTitle:atIndex: 則把使用者點擊的索引對應到實際 section。這樣就能完成索引搜尋體驗。
UILocalizedIndexedCollation 的價值在於它不只做排序,還能處理本地化索引。對於需要大量清單的 App,它能顯著提升瀏覽效率。