[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
}
}
Author And Source
이 문제에 관하여([Swift] - Section Index Title), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@din0121/Section-Index-Title저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)