您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS UITableView 進階:用 UILocalizedIndexedCollation 實作索引搜尋

編輯:iOS 開發綜合

大量資料為什麼需要索引

當 UITableView 顯示大量資料,例如時區、城市或聯絡人,單靠垂直捲動會讓使用者難以快速定位。UILocalizedIndexedCollation 是 iOS 提供的索引工具,能依當地語系規則把資料分組並排序,再搭配右側索引列使用。

資料包裝與 collation

範例以時區為主題,建立 ZYTimeZoneWrapper 物件保存 localeName。在 configureSections 中,先取得 UILocalizedIndexedCollation 的 currentCollation,再用 sectionTitles 建立對應的 section 陣列。接著透過 sectionForObject:collationStringSelector: 判斷每筆資料屬於哪一區,最後用 sortedArrayFromArray:collationStringSelector: 排序。

表格方法的對應

numberOfSectionsInTableView: 回傳 section 數量;titleForHeaderInSection: 回傳區段標題;sectionIndexTitlesForTableView: 提供右側索引;sectionForSectionIndexTitle:atIndex: 則把使用者點擊的索引對應到實際 section。這樣就能完成索引搜尋體驗。

小結

UILocalizedIndexedCollation 的價值在於它不只做排序,還能處理本地化索引。對於需要大量清單的 App,它能顯著提升瀏覽效率。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved