Skip to content

Latest commit

 

History

History
170 lines (136 loc) · 7.81 KB

1. UITableView.md

File metadata and controls

170 lines (136 loc) · 7.81 KB

날짜: 2023-02-07 20:17

주제: 리스트 형태의 정보를 담기 위해


메모:

UITableView

  • 테이블뷰는 iOS 애플리케이션에서 많이 활용하는 사용자 인터페이스이다.
  • 테이블뷰는 리스트 형태를 지니고 있으며 스크롤이 가능해 많은 정보를 보여줄 수 있다.
  • 테이블 뷰의 각 셀에 나타나며, 셀은 일반적으로 사용자에게 정보를 제공하거나, 사용자의 입력을 받기 위한 인터페이스 요소를 포함한다.
기본 형태
  • 테이블뷰는 하나의 열(column)과 여러 줄의 행(row)을 지니며, 수직으로만 스크롤이 가능하다.
  • 각 행은 하나의 셀(cell)에 대응한다.
  • 섹션(section)을 이용해 행을 시각적으로 나눌 수 있다.
  • 헤더(header)와 푸터(footer)에 이미지나 텍스트를 추가해 추가 정보를 보여줄 수 있다.
주요 스타일
  • Plain TableView
    • 더 이상 나뉘지 않는 연속적인 행의 리스트 형태이다.
    • 하나 이상의 섹션을 가질 수 있으며, 각 섹션은 여러 개의 행을 지닐 수 있다.
    • 각 섹션은 헤더 혹은 푸터를 옵션으로 지닐 수 있다.
    • 색인을 이용한 빠른 탐색을 하거나 옵션을 선택할 때 용이하다.
  • Grouped TableView
    • 섹션을 기준으로 그룹화되어있는 리스트 형태이다.
    • 하나 이상의 섹션을 가질 수 있으며, 각 섹션은 여러 개의 행을 지닐 수 있다.
    • 각 섹션은 헤더 혹은 푸터를 옵션으로 지닐 수 있다.
    • 정보를 특정 기준에 따라 개념적으로 구분할 때 적합하다.
    • 사용자가 정보를 빠르게 이해하는 데 도움이 된다.
테이블뷰 생성
  • Dynamic Prototypes
    • 셀 하나를 디자인해 이를 다른 셀의 템플릿으로 사용하는 방식
    • 같은 레이아웃의 셀을 여러 개 이용해 정보를 표시할 경우 활용
    • 데이터 소스(UITableViewDataSource) 인스턴스에 의해 콘텐츠를 관리하며, 셀의 개수가 상황에 따라 변하는 경우에 사용
  • Static Cells
    • 고유의 레이아웃과 고정된 수의 행을 가지는 테이블뷰에 사용
    • 테이블뷰를 디자인하는 시점에 테이블의 형태와 셀의 개수가 정해져 있는 경우 사용
    • 셀의 개수가 변하지 않음
프로토콜
  • UITableViewDataSource
    • 테이블 뷰에 표시될 데이터를 관리한다. 이를 통해 테이블 뷰에 표시할 행의 수와 각 행에 표시할 셀을 제공할 수 있다.
    • 또한, 옵션으로 섹션의 수, 섹션 헤더와 푸터의 제목 등을 제공할 수 있다.
    • 데이터 소스는 데이터 모델의 델리게이트로, 테이블뷰의 시각적 모양에 대한 최소한의 정보를 제공한다.
     	 @required 
     	 // 특정 위치에 표시할 셀을 요청하는 메서드
     	 func tableView(UITableView, cellForRowAt: IndexPath) 
     	 
     	 // 각 섹션에 표시할 행의 개수를 묻는 메서드
     	 func tableView(UITableView, numberOfRowsInSection: Int)
     	 
     	 @optional
     	 // 테이블뷰의 총 섹션 개수를 묻는 메서드
     	 func numberOfSections(in: UITableView)
     	 
     	 // 특정 섹션의 헤더 혹은 푸터 타이틀을 묻는 메서드
     	 func tableView(UITableView, titleForHeaderInSection: Int)
     	 func tableView(UITableView, titleForFooterInSection: Int)
     	 
     	 // 특정 위치의 행을 삭제 또는 추가 요청하는 메서드
     	 func tableView(UITableView, commit: UITableViewCellEditingStyle, forRowAt: IndexPath)
     	 
     	 // 특정 위치의 행이 편집 가능한지 묻는 메서드
     	 func tableView(UITableView, canEditRowAt: IndexPath)
     	
     	 // 특정 위치의 행을 재정렬 할 수 있는지 묻는 메서드
     	 func tableView(UITableView, canMoveRowAt: IndexPath)
     	 
     	 // 특정 위치의 행을 다른 위치로 옮기는 메서드
     	 func tableView(UITableView, moveRowAt: IndexPath, to: IndexPath)
  • UITableViewDelegate
    • 테이블 뷰에 발생하는 사용자의 동작을 관리한다.
    • 이를 통해 셀 션택, 행 높이, 섹션 헤더와 푸터의 뷰 등을 구성할 수 있다.
    • 델리게이트는 테이블뷰의 시각적인 부분 수정, 행의 선택 관리, 액세서리뷰 지원 그리고 테이블뷰의 개별 행 편집을 도와준다.
    • 델리게이트 메서드를 활용하면 테이블뷰의 세세한 부분을 조정할 수 있다.
     	// 특정 위치 행의 높이를 묻는 메서드
     	 func tableView(UITableView, heightForRowAt: IndexPath)
     	 // 특정 위치 행의 들여쓰기 수준을 묻는 메서드
     	 func tableView(UITableView, indentationLevelForRowAt: IndexPath)
     	
     	 // 지정된 행이 선택되었음을 알리는 메서드
     	 func tableView(UITableView, didSelectRowAt: IndexPath)
     	
     	 // 지정된 행의 선택이 해제되었음을 알리는 메서드
     	 func tableView(UITableView, didDeselectRowAt: IndexPath)
     	
     	 // 특정 섹션의 헤더뷰 또는 푸터뷰를 요청하는 메서드
     	 func tableView(UITableView, viewForHeaderInSection: Int)
     	 func tableView(UITableView, viewForFooterInSection: Int)
     	
     	 // 특정 섹션의 헤더뷰 또는 푸터뷰의 높이를 물어보는 메서드
     	 func tableView(UITableView, heightForHeaderInSection: Int)
     	func tableView(UITableView, heightForFooterInSection: Int)
     	
     	 // 테이블뷰가 편집모드에 들어갔음을 알리는 메서드
     	 func tableView(UITableView, willBeginEditingRowAt: IndexPath)
     	
     	 // 테이블뷰가 편집모드에서 빠져나왔음을 알리는 메서드
     	 func tableView(UITableView, didEndEditingRowAt: IndexPath?)

예시

import UIKit

class ViewController: UIViewController {
    let data = ["1","2","3"]

    var tableView = UITableView(frame: .zero, style: .grouped)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .systemBackground
        
        tableView.dataSource = self
        tableView.delegate = self
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
        
        view.addSubview(tableView)
        
        tableView.translatesAutoresizingMaskIntoConstraints = false
        
        tableView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
        tableView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
        tableView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
        tableView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
    }

}


extension ViewController: UITableViewDelegate {
    
}

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

출처(참고문헌)

연결문서

Tag

  • #IOS/UIKit/TableView