k-coding

Xcode 테이블 뷰 ( 2 ) 본문

iOS/Xcode

Xcode 테이블 뷰 ( 2 )

chkhn_oiiu 2021. 11. 23. 15:59

Xcode 테이블 뷰 ( 2 )

 

 

테이블 뷰 데이터

 

UITableView는 데이터 소스델리게이트가 꼭 필요합니다.

 

데이터 소스가 Model의 역할을 하고,

 

테이블 뷰가 View,  델리게이트가 Controller의 역할을 담당합니다.

 

이 세가지가 같이 존재하여야 MVC프로그래밍 디자인 패턴에 의하여 테이블 뷰가 잘 작동될 수 있습니다. 

 

테이블 뷰자체로는 데이터의 표시만 관리하고 데이터 자체를 관리하지 않습니다.

 

따라서 데이터를 관리하기 위해선 UITableViewDataSource 프로토콜을 구현하는 오브젝트가 필요합니다.

 

이 오브젝트를 데이터 소스라고 부릅니다.

 

데이터 소스

 

데이터 소스는 데이터 모델의 델리게이트로써, 테이블 뷰의 시각적인 정보와

 

테이블 뷰의 각 셀을 생성하고 수정하는데 필요한 정보를 제공합니다.

 

 

// 테이블 뷰의 행 갯수 리턴    
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
   return 0
}

// 테이블 뷰의 각 셀의 정보 
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   // 적절한 셀의 유형 
   let cell = tableView.dequeueReusableCell(withIdentifier: "cellTypeIdentifier", for: indexPath)
   
   // 셀의 내용 입력
   cell.textLabel!.text = "Cell text"
       
   return

 

관련 메소드

 @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)

 

델리게이트

 

섹션을 관리하는 메소드로 header와 footer 구성, 셀 삭제와 재배치, 테이블 뷰에서 다른 많은 작업을 관리합니다.

 

보통 테이블 뷰의 디자인적인 요소나 악세서리 뷰 등 기타 작업을 관리해줍니다.

 

관련 메소드

// 특정 위치 행의 높이를 묻는 메서드
 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?)

'iOS > Xcode' 카테고리의 다른 글

Xcode 오토레이아웃 (AutoLayout) (2)  (0) 2022.01.11
Xcode 오토레이아웃 (AutoLayout) (1)  (0) 2022.01.10
Xcode 테이블 뷰 ( 1 )  (0) 2021.11.22
Sender 와 tag  (0) 2021.11.12
Xcode 오토 레이아웃 (Auto Layout) ( 1 )  (0) 2021.11.08
Comments