TableView를 동작 방식과 화면에 Cell을 출력하기 위해 최소한 구현해야 하는 DataSource 메서드를 설명하시오.
Opened this issue · 2 comments
inuinseoul commented
TableView를 동작 방식과 화면에 Cell을 출력하기 위해 최소한 구현해야 하는 DataSource 메서드를 설명하시오.
sustainable-git commented
- UITableView는 하나의 수직 열에 여러 행의 data을 표현하기 위한 View입니다. 각 행은 app의 content를 표현합니다.
- UITableView는 data를 어떻게 보여줄지만 관리할 뿐, data 그 자체는 관리하지 않습니다. data를 관리하기 위해서는 datasource object를 제공해야 합니다.
- datasource의 책임은 다음과 같습니다.
- section의 갯수와 table 내의 row의 갯수를 보고
- table의 각 row에 해당하는 cell을 제공
- section header와 footer 제목을 제공
- table의 index 구분
- data 변경에 따른 update에 반응
- UITableView의 Cell을 출력하기 위해서는 최소한 2개의 Datasource methods를 구현해야 합니다.
- tableView(_, numberOfRowsInSection) -> Int
- 주어진 section에 몇 개의 rows가 존재하는지를 반환하는 method입니다.
- tableView(_, cellForRowAt) -> UITableViewCell
- 어떤 위치에 cell이 추가될지 정의하는 method입니다.
- tableView(_, numberOfRowsInSection) -> Int
inuinseoul commented
iOS의 UITableView는 하나의 열에 세로로 스크롤되는 콘텐츠 행들을 표시합니다. 테이블의 각 행에는 앱 콘텐츠의 일부분이 표함됩니다. 예를 들어 연락처앱은 각 연락처의 이름을 별도의 행에 표시합니다. 또 설정앱은 사용가능한 설정 그룹이 행으로 표시됩니다. 하나의 긴 행을 표시하도록 테이블을 구성하거나 관련 행을 섹션형태로 그룹화하여 콘텐츠를 더 쉽게 탐색할 수도 있습니다.
UITableView은 cell을 표시하고 데이터를 주입하는 행위에 대한 책임을 UITableViewDataSource에게 위임합니다. 따라서 UITableViewDataSource를 사용하고 적절한 메서드를 정의해줘야 데이터를 출력할 수 있습니다. 이 때 필수적으로 사용해야하는 메서드는 func numberOfSections(in tableView: UITableView)
와 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
입니다.
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Int
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return UITableViewCell
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int)
에는 각 section에 들어갈 행의 개수를 리턴하고, func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
에는 cell을 구성하여 리턴해줍니다.