Notice
Recent Posts
Recent Comments
Link
大器晩成
TableViewController - Dynamic , Static 본문
스위프트에서 많이 사용되는 테이블 뷰 컨트롤러에 대해서 정리해 보도록 하겠습니다.
애플 문서에는 "테이블 관리를 전문으로 하는 뷰컨트롤러라고 정의하고 있습니다.
테이블뷰는 Content, View, Style로 구성되어 있습니다.
Contet - Dynamic Prototypes, Static Cell
View - Header / Footer
Style - Plain, Grouped, Inset Grouped
[Content]
Static Cell | Dynamic Prototypes | |
셀 개수 | 미리 정해진 개수 | 동적으로 변함 |
설정 방법 | 스토리보드에서 직접 배치 | - UITableViewDataSource에서 설정 - 스토리보드에서도 설정은 가능 |
[Style]
스타일 | 특징 | 섹션 헤더 |
Plain | 기본 리스트 스타일 | 스크롤 시 사라짐 |
Grouped | 섹션별 박스로 구분 | 스크롤 시 고정 |
Inset Grouped | Grouped + 좌우 여백 | 스크롤 시 고정 |
테이블 뷰 셀(시스템 셀)은 기본적으로 4가지 셀 스타일이 있습니다.
스타일 | 설명 |
default | 텍스트 하나만 표시됨 |
subtitle | 제목 + 작은 부제목이 표시됨 |
Letft Detail (value1) | 왼쪽에 제목, 오른쪽에 작은 텍스트 표시 |
Right Detail (value2) | 왼쪽 텍스트는 작은 폰트, 오른쪽이 강조됨 |
- 스토리보드에서 만든 시스템 셀을 사용할 때에는, Identifier를 잘 써야 합니다. Identifier가 매칭이 안될 경우 오류가 발생할 수 있습니다.
[참고]
class TableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// 섹션의 개수
return 2
}
// MARK: - Header 설정
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if section == 0 {
return "오늘 뭐할래용?"
} else {
return "이번주는 뭐했나요??"
}
}
// MARK: - Footer 설정
override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
if section == 0 {
return "투드리스트를 통해 하루하루를 개선해봅시다"
} else {
return "계획은 잘 이루어지셨나요?"
}
}
// MARK: - 섹션별 셀의 개수 설정
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return 2 //섹션별 셀의 개수
} else {
return 3
}
}
// MARK: - 셀 설정
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// 아까 작성한 Identifier
let cell = tableView.dequeueReusableCell(withIdentifier: "systemCell") ?? UITableViewCell()
if indexPath.section == 0 {
cell.textLabel?.text = "오늘 할일은?"
cell.detailTextLabel?.text = "공부?"
} else {
cell.textLabel?.text = "이번주 할일은?"
cell.detailTextLabel?.text = "술울?"
}
return cell
}
// MARK: - 헤더 높이 50pt
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
// MARK: - 푸터 높이 50pt
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 50
}
}
- 혹여, 왜 Delegate나 DataSource를 사용하지 않았냐고 묻는 독자가 있으시다면, UITableViewControllerUITableViewDelegate, UITableViewDataSource를 자체적으로 채택하고 있습니다,
- 또한 UITableViewController가 기본적으로 delegate & Datasource 역할을 하고 있습니다.
728x90
'iOS > UiKit' 카테고리의 다른 글
UIStackView (0) | 2025.01.05 |
---|---|
Table View Controller - 2탄 (실전편) (0) | 2025.01.05 |
Auto Layout (0) | 2025.01.02 |
View Controller 생명주기(상태변화) (0) | 2025.01.01 |
AppDelegate, Scene Delegate (1) | 2024.12.30 |