UIKit - UITableView Section Header 설정하기
UIKit - UITableView Section Header 설정하기
예시 코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import UIKit
final class BookListViewController: UIViewController {
// Section Header 배열 정의
private let tableViewSectionHeader = ["책 기록"]
}
extension BookListViewController: UITableViewDataSource, UITableViewDelegate{
// Section 수
func numberOfSections(in tableView: UITableView) -> Int {
return tableViewSectionHeader.count
}
// Section Title Text
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return tableViewSectionHeader[section]
}
// Section Title 폰트 설정
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let titleLabel = UILabel()
titleLabel.font = UIFont.boldSystemFont(ofSize: 24)
titleLabel.textColor = .white
titleLabel.text = self.tableView(tableView, titleForHeaderInSection: section)
titleLabel.textAlignment = .left
let headerView = UIView()
headerView.addSubview(titleLabel)
headerView.backgroundColor = .background
titleLabel.snp.makeConstraints { make in
make.centerY.equalToSuperview()
make.leading.equalToSuperview().inset(5)
}
return headerView
}
// Header 높이 설정
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
}
결과
Reference
This post is licensed under CC BY 4.0 by the author.
