Apr 9, 2021, TIL (Today I Learned) - UITableView, Cell, DataSource, Delegate

Inwoo Hwang·2021년 8월 26일
0
post-thumbnail

학습 내용


UITableView란?

iOS 애플리케이션에서 정보를 리스트 형태로 보여주기 위해 사용하는 사용자 인터페이스 입니다.

UITableView Cell이란?

UITableViewCell은 UIView를 상속 받는다

테이블뷰의 셀은 각 테이블 뷰를 이루는 개별적인 행(row)을 뜻한다.

당연하게 들릴지도 모르지만 테이블뷰 셀은 UITableViewCell Class를 상속 받는다.

테이블 뷰 셀의 구조

표준 테이블 뷰셀은 기본적으로 컨텐츠 영역만 포함하거나 악세서리 뷰 영역이 포함될 수 있다.

  • 컨텐츠 영역: 문자열, 이미지 또는 고유 식별자
  • 악세서리 뷰 영역: 상세보기, 재정렬, 스위치와 같은 컨트롤 객체

[출처]: UITableViewCell | Apple Developer Documentation

편집 모드 같은 경우 다음과 같은 기능이 추가될 수 있습니다.

  • 삭제컨트롤
  • 추가 컨트롤
  • 재정렬 컨트롤(악세서리 뷰)

테이블뷰 셀의 기본 기능

  • textLabel: UILabel: 주 제목 레이블
  • detailTextLabel: UiL: 추가 세부사항 표시
  • imageView: 이미지 표시

UITableView 객체는 DataSourceDelegate가 없다면 정상적으로 동작하기 어렵습니다. 이 두 프로토콜은 UITable뷰의 비서와 같은 역할을 하며 UITableView가 동작하도록 각각의 전공 분야를 살립니다

IndexPath(for:)

func indexPath(for cell: UITableViewCell) -> IndexPath?
class ItemTableViewController: UITableViewController {
    var items: [Item] = []
.
.
.
guard let cell = self.tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? ItemTableViewCell else {
            return UITableViewCell()
        }
        cell.itemName.text = items[indexPath.row].name
}

indexPath는 tableView의 행을 식별하는 인덱스 경로입니다.

cell.itemName.text = items[indexPath.row].name

위 코드를 이해해 보면

tableview 행의 위치를 인덱스로 가지는

items라는 배열의 원소 중 name이라는 key를 가지는 value를

itemName의 text로 써주겠다고 이해할 수 있습니다.

UITableView DataSource

  • DataSource 객체는 UITableViewDataSource Protocol을 채택합니다.
  • 테이블뷰의 셀을 생성하고 수정하는데 필요한 데이터 를 UITableView에게 제공합니다.
  • UIViewController가 해당 프로토콜을 채택하면 UITableViewDataSorce에게 위임하는 방식으로 아래와 같은 기능을 수행할 수 있다.
 @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)

[출처]: iOS 앱 프로그래밍 > 3) DataSource와 Delegate? : boostcourse

UITableViewDelegate

  • TableViewDelegate 객체는 UITableViewDelegate Protocol 을 채택합니다.
  • 테이블 뷰의 시각적인 부분 수정, 개별 행 편집, 행의 선택관리, 악세서리 뷰 지원과 같은 기능을 하는 것을 도와줍니다.
  • UIViewController가 해당 프로토콜을 채택하면 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?)

[출처]: iOS 앱 프로그래밍 > 3) DataSource와 Delegate? : boostcourse

[참고자료]

UITableViewCell | Apple Developer Documentation

UITableViewDataSource | Apple Developer Documentation

UITableViewDelegate | Apple Developer Documentation

iOS 앱 프로그래밍 > 2) 테이블뷰 셀이란? : boostcourse

TableView 코드 중 indexPath에 대해 공부 | VincentGeranium Blog

Mind Map


profile
james, the enthusiastic developer

0개의 댓글