티스토리 뷰

반응형

Apple Developer Documentation - UITableViewDataSource

UITableViewDataSource

테이블 뷰의 셀에 사용되는 데이터를 관리하기 위해 채택하는 프로토콜

Declaration

protocol UITableViewDataSource

Overview

테이블 뷰는 데이터를 보여주기만 하는 것이지 자체적으로 데이터를 관리할 수는 없다. 데이터를 관리하기 위해서는 UITableViewDataSource 프로토콜을 사용해야 한다. data source object는 테이블에서 데이터와 관련된 요청이 오면 응답하며 테이블의 데이터를 직접 관리하거나 앱의 다른 부분과 조정하여 해당 데이터를 관리한다. data source object의 다른 기능은 다음과 같다.

 

1. 테이블의 섹션 수와 행 수를 알려준다.

2. 테이블의 각 행마다 셀을 제공한다.

3. 섹션의 header와 footer에 타이틀을 제공한다.

4. 사용자나 테이블의 데이터가 변경되었으면 업데이트해준다.

 

프로토콜 사용을 위해 준수해야할 규칙에는 2개의 메서드만 필요하다.

// Return the number of rows for the table.     
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
   return 0
}

// Provide a cell object for each row.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   // Fetch a cell of the appropriate type.
   let cell = tableView.dequeueReusableCell(withIdentifier: "cellTypeIdentifier", for: indexPath)

   // Configure the cell’s contents.
   cell.textLabel!.text = "Cell text"

   return cell
}

위의 코드에서 사용된 2개의 메서드가 프로토콜 사용을 위해 반드시 필요한 메서드이다.

2개의 메서드 말고도 메서드가 몇 개 더 있지만 이는 특별한 상황에 사용하면 된다.

테이블의 셀을 data source object를 사용해 만들고 싶다면 아래 자료를 확인해보자.

 

Apple Developer Documentation - filling a table with data

 

Specifying the Location of Rows and Sections

테이블 뷰의 셀은 NSIndexPath 객체의 row, section 프로퍼티로 위치를 알 수 있다. row, section은 제로베이스 인덱스로 0부터 시작한다. 당연한 말이지만 section을 설정해두지 않았다면 row의 인덱스만 존재하게 된다.

 

UITableViewDataSource 프로토콜의 메서드

테이블에서 행과 섹션의 수를 알려주는 메서드

func tableView(UITableView, numberOfRowsInSection: Int) -> Int
// 테이블 뷰의 section의 index를 가지고 해당 sections에 포함된 셀수를 알려주는 메서드

func numberOfSections(in: UITableView) -> Int
// 테이블 뷰의 전체 section수를 알려주는 메서드

셀에 Header와 Footer를 제공하는 메서드

func tableView(UITableView, cellForRowAt: IndexPath) -> UITableViewCell
// 테이블 뷰에서 특정 index에 있는 셀을 알려준다.

func tableView(UITableView, titleForHeaderInSection: Int) -> String
// 테이블 뷰에서 특정 section의 header 타이틀을 가져오는 메서드

func tableView(UITableView, titleForFooterInSection: In) -> String
// 테이블 뷰에서 특정 section의 footer 타이틀을 가져오는 메서드

테이블 뷰에 셀을 추가하거나 제거를 적용하는 메서드

func tableView(UITableView, commit: UITableViewCell.EditingStyle, forRowAt: IndexPath)
// 셀을 삭제 또는 삽입 후 이를 적용하기 위한 메서드
// 실제 삽입과 삭제는 UITableView의 insertRows(at:with:), deleteRows(at:with:) 메서드가 수행

func tableView(UITableView, canEditRowAt: IndexPath) -> Bool
// 주어진 셀이 수정가능 상태인지 알려준다.

테이블 뷰의 셀을 재배치하는 메서드

func tableView(UITableView, canMoveRowAt: IndexPath) -> Bool
// 주어진 index의 셀이 다른 index로 이동가능한 상태인지 알려준다.

func tableView(UITableView, moveRowAt: IndexPath, to: IndexPath)
// 테이블 뷰의 셀을 index를 기반으로 다른곳으로 이동시킨다. 

section의 인덱스와 관련된 메서드

func sectionIndexTitles(for: UITableView) -> [String]?
// 테이블 뷰의 모든 섹션의 타이틀을 가져온다

func tableView(UITableView, sectionForSectionIndexTitle: String, at: Int) -> Int
// sectionIndexTitles에서 반환된 Array에서 타이틀과 타이틀의 Array에서의 index를 가지고
// 특정 section의 index를 알려준다.

 

반응형
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
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
글 보관함