2017-04-10 1 views
0

私は数百ものセクションを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メソッドでは、垂直リストの英数字をタップすると、指定された文字または数字の最初のセクションに移動し、インデックスの作成時に大文字と小文字を区別しないようにします。

答えて

0

私がしなければならなかったのは、項目の最初の大文字がタップされた文字と等しい条件に一致する最初の項目のインデックスがsectionsArrayであることでした。 :

override func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int { 
    return sectionsArray.index(where: { String($0.characters.first!).uppercased() == title }) 
} 
関連する問題