Use header and footer views as visual markers for the beginning and end of sections. Header and footer views are optional, and you can customize them as much or as little as you want.
: header와 footer는 section의 시작과 끝의 시각적으로 보여줄 때 사용
section의 구성
>> Header + Cell(여러개 가능) + Footer
// header와 footer의 declaration
// Create a standard header that includes the returned text.
override func tableView(_ tableView: UITableView, titleForHeaderInSection
section: Int) -> String? {
return "Header \(section)" //header의 title 반환
}
// Create a standard footer that includes the returned text.
override func tableView(_ tableView: UITableView, titleForFooterInSection
section: Int) -> String? {
return "Footer \(section)" //footer의 title 반환
}
Section 사용
- Cell 이용과 동일하게 delegate에서 Section의 개수와 header에 관한 함수, footer에 관한 함수에 각각 데이터 반환하여 사용
extension ViewController: UITableViewDelegate, UITableViewDataSource {
//section 나누기
func numberOfSections(in tableView: UITableView) -> Int {
return sectionHeader.count
} //section이 몇개 필요한지 데이터값을 받아와 카운트하는 과정
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sectionHeader[section]
}
func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
return sectionFooter[section]
}
// cell 만들기
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cellDataSource.count
} //cell 갯수
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
cell.textLabel?.text = cellDataSource[indexPath.row]
return cell
} // cell 생성
}
TableView는 메모리의 효율과 성능 극대화를 위해 재사용 메커니즘을 사용
TableView는 내부적으로 재사용 큐를 관리하고, Cell이 요청할 때마다 저장되어 있는 큐에 존재하는 Cell을 반환
만약 큐에 Cell이 존재하지 않는다면 PrototypeCell을 기반으로 새로 만들어 반환
>> 셀이 100개가 있다면 100개를 미리 만들어두는 것이 아니라 필요할 때마다(화면 전환 등) cell 반환
Cell을 생성할 때는 tableView의 dequeueReusableCell(withldentifier:for:) 메서드를 사용
parameter로 앞서 지정한 PrototypeCell의 identifier와 메서드에서 전달받은 indexPath 사용
Cell의 위치는 Section 인덱스와 Row 인덱스를 조합해 사용하고, 이들은 각각 section과 Row 속성에 존재
텍스트는 배열에 저장된 값
배열에 indexPath의 row 속성을 전달해 해당하는 원소를 가져와 cell에 표시한 뒤 이를 반환
https://chillog.page/50에서 인용
section style
'FE 공부 > Swift' 카테고리의 다른 글
Swift - TableView _ Edit Mode (0) | 2022.11.29 |
---|---|
Swift - TableView _ Managing Selection (0) | 2022.11.29 |
Swift - Alert Controller (0) | 2022.11.22 |
Swift _ StackView (0) | 2022.11.15 |
Swift _ ProgressView (0) | 2022.11.15 |
댓글