私は数百ものセクションをUITableViewに持っています。すべてのセクションを配列に入れました。大文字のみを含むように配列を変更し、繰り返し文字を削除しました。UITableViewのセクションタイトルタイトルsectionForSectionIndexTitleメソッドでA-Zを使用する
//sample array
let sectionsArray: [String] = [Apple, Asparagus, Banana, Barley, Cucumber...]
var firstLettersOfSections: [String] = []
firstLettersOfSections = sectionsArray.map { String($0.characters.first!).uppercased() }
firstLettersOfSections = removeDuplicates(&firstLettersOfSections)
私はsectionIndexTitles
方法でfirstLettersOfSections
を返しました:
override func sectionIndexTitles(for tableView: UITableView) -> [String]? {
return firstLettersOfSections
}
をこのコードはのtableViewの右側に適切な英数字を生成文字が上のタップされたとき、しかし、彼らはに行きますfirstLettersOfSections
のインデックスに対応するセクションインデックス。
sectionForSectionIndexTitle
tableViewメソッドでは、垂直リストの英数字をタップすると、指定された文字または数字の最初のセクションに移動し、インデックスの作成時に大文字と小文字を区別しないようにします。