[Swift] - Section Index Title

20450 단어 Table ViewTable View

↓ Single Selection

import UIKit

class ViewController: UIViewController {

    let list = Region.generateDate()
    
    @IBOutlet weak var listTableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

extension ViewController: UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        list.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return list[section].contries.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        
        let target = list[indexPath.section].contries[indexPath.row]
        cell.textLabel?.text = target
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return list[section].title
    }
}

extension ViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
        return indexPath
    }
}

Section Index Title

오른쪽에 보면 초성이 있는데 그것을 Section Index Title이라고 합니다.

func sectionIndexTitles(for tableView: UITableView) -> [String]? {
        return stride(from: 0, to: list.count, by: 1).map { list[$0].title }
    }

  • 섹션 인덱스의 타이틀 디자인을 변경, 폰트 디자인, 크기를 변경하는 것을 불가능합니다.
    override func viewDidLoad() {
        super.viewDidLoad()
        listTableView.sectionIndexColor = UIColor.blue
        listTableView.sectionIndexBackgroundColor = UIColor.lightGray
        listTableView.sectionIndexTrackingBackgroundColor = UIColor.systemPink 
    }

⌨️ 구현

import UIKit

class ViewController: UIViewController {

    let list = Region.generateDate()
    
    @IBOutlet weak var listTableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        listTableView.sectionIndexColor = UIColor.blue
        listTableView.sectionIndexBackgroundColor = UIColor.lightGray
        listTableView.sectionIndexTrackingBackgroundColor = UIColor.systemPink
    }
}

extension ViewController: UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        list.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return list[section].contries.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        
        let target = list[indexPath.section].contries[indexPath.row]
        cell.textLabel?.text = target
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return list[section].title
    }
    
    func sectionIndexTitles(for tableView: UITableView) -> [String]? {
        return stride(from: 0, to: list.count, by: 1).map { list[$0].title }
    }
    
    func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
        return index
    }
}

extension ViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
        return indexPath
    }
}

좋은 웹페이지 즐겨찾기