[iOS] 테이블뷰에서 섹션별로 초성 인덱스 표시하는 방법

RudinP·2024년 4월 16일
0

Study

목록 보기
222/226

sectionIndexTitles & sectionForSectionIndexTitle


    func sectionIndexTitles(for tableView: UITableView) -> [String]? {
        let consonantsAndAlphabets: [String] = ["ㄱ", "ㄴ", "ㄷ", "ㄹ", "ㅁ", "ㅂ", "ㅅ", "ㅇ", "ㅈ", "ㅊ", "ㅋ", "ㅌ", "ㅍ", "ㅎ", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]

        return consonantsAndAlphabets
    }
    //이 부분의 경우 인덱스에 해당하는 Int를 리턴하면 상단의 sectionIndexTitles에서 매칭하여 보여준다. 이부분을 구현하지 않으면 자동으로 테이블뷰의 인덱스별로 상단의 인덱싱이 0번부터 매칭되어 작동한다.
    func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
        return list.firstIndex(where: {$0.title.uppercased() == title.uppercased()}) ?? 0
    }
profile
곰을 좋아합니다. <a href = "https://github.com/RudinP">github</a>

0개의 댓글