iOS랑 친해지기

[iOS] TableView 메서드 정리 (Swift)

데브킹덕 2022. 10. 6. 14:14

테이블 뷰??

- 데이터를 목록 형태로 보여줄 수 있는 가장 기본적인 UI컴포넌트

 

특징

  • ScollView를 상속받아 스크롤을 할 수 있어 많은 정보의 데이터를 제공할 수 있음
  • 여러 개의 Cell을 가지고 있음
  • 하나의 열과 여러개의 줄을 가지고 있어 수직으로만 스크롤할 수 있음
  • 섹션을 이용해 행을 그룹화하여 콘텐츠를 좀 더 쉽게 탐색할 수 있음
  • 섹션의 헤더와 푸터에 뷰를 구성하여 추가적인 정보를 구성할 수 있음
  • Delegate와 DataSource 프로토콜을 채택하여 구현해줘야 함 

 

 

Delegate & DataSource

 

DataSorce  

- TableView를 생성하고 수정하는데 필요한 정보를 테이블 뷰 객체에 제공

- 즉 데이터를 받아와 뷰를 그려줌 

- 총 섹션이 몇개??,행 몇개??, 행이 어떤정보를 표시할지??

- numberofRowsInSection (각 섹션에 표시할 행의 갯수) 메서드 반드시 구현필요

- cellForRowAt(Index의 Row의 Cell에 대한 정보를 넣어 Cell을 반환) 메서드 반드시 구현 필요

 

 

사용가능 메서드 

 

 

 

 

 

Delegate

- TableView의  시각적인 부분을 설정하고, 행의 액션 관리, 액세서리 뷰 지원, 테이블 뷰의 개별 행 편집을 도와줌

- TableView의 동작과 외관을 담당

- View가 변경되는 사항을 Delegate가 담당

- View는 Delegate에 의존하여 View를 업데이트한다고 생각하자
- 행의 높이, 행 선택시 어떤 행위를 할지 ??

- 필수로 필요한 메서드는 없음

 

 

메서드 정리

//
//  TestTableViewController.swift
//  Calculator
//
//  Created by duck on 2022/10/06.
//

import UIKit

class TestTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    // MARK: - Table view data source
    
    //총 섹션의 갯수
    override func numberOfSections(in tableView: UITableView) -> Int {
        
        return 0
    }

    //각 섹션에 표시할 행의 갯수
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      
        return 0
    }

    
    // 특정 인덱스 Row의 Cell에 대한 정보를 넣어 Cell을 반환하는 메서드
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
        // Configure the cell...
        return cell
    }
    
    //특정 섹션의 헤더 타이틀을 묻는 메서드
    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        <#code#>
    }
    
    //특정 섹션의 풋터 타이틀을 묻는 메서드
    
    override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
        <#code#>
    }
    
    
    //특정 위치 행이 편집이 가능한지 묻는 메서드
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        <#code#>
    }
    
    //특정 위치의 행을 재정렬 할 수 있는지 묻는 메서드
    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        <#code#>
    }
    
    //테이블뷰 섹션 인덱스 타이틀을 묻는 메서드
    override func sectionIndexTitles(for tableView: UITableView) -> [String]? {
        <#code#>
    }
    
    //인덱스에 해당하는 섹션을 알려주는 메서드
    override func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
        <#code#>
        
    }
    
    //스와이프 ,편집 모드에서 선택하면 호출되는 메서드
    //해당 메서드에서는 행의 변경사항을 Commit해야 함
    override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
        <#code#>
    }
    
    
    //행이 다른 위치로 이동되면 어디에서 어디로 이동되었는지 알려주는 메서드
    override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
        <#code#>
    }

 
}
//MARK: Delegate
    
    //행이 선택되었을때 호출되는 메서드
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        <#code#>
    }
    
    
    //행이 해제되었을때 호출되는 메서드
    override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        <#code#>
    }
    
    //특정 위치 행의 높이를 묻는 메서드
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        <#code#>
    }
    
    
    //지정된 섹션 헤더뷰에 표시할 뷰가 어떤것인지 묻는 메서드
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        <#code#>
    }
    
    //지정된 섹션 풋터뷰에 표시할 뷰가 어떤것인지 묻는 메서드
    override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        <#code#>
    }
    
    //지정된 섹션 헤더뷰의 높이를 묻는 메서드
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        <#code#>
    }
    
    //지정된 섹션 풋터뷰의 높이를 묻는 메서드

    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        <#code#>
    }
 
    //테이블 뷰가 편집 모드에 들어갔을 때 호출되는 메서드
    override func tableView(_ tableView: UITableView, willBeginEditingRowAt indexPath: IndexPath) {
        <#code#>
    }
    
    //테이블 뷰가 편집 모드에서 빠져나왔을때 호출되는 메서드
    override func tableView(_ tableView: UITableView, didEndEditingRowAt indexPath: IndexPath?) {
        <#code#>
    }
    
    
    //테이블 뷰가 셀을 사용하여 행을 그리기 직전에 호출되는 메서드
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        <#code#>
    }
    
    //테이블 뷰로부터 셀이 화면에 사라지면 호출되는 메서드
    override func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        <#code#>
    }