관리 메뉴

java,javascript,android,php,sql,공부용,메모용

테이블뷰 TableView / UITableView, UITableDataSource, UITableDelegate / Xcode SwiftUI 본문

모바일/SwiftUI ios 공부

테이블뷰 TableView / UITableView, UITableDataSource, UITableDelegate / Xcode SwiftUI

yy_dd2 2021. 12. 10. 04:22
반응형

UITableView

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

 

- 여러개의Cell을가지고있고하나의열과여러줄의행을지 니고 있으며, 수직으로만 스크롤 가능합니다. 

- 섹션을이용해행을그룹화하여콘텐츠를좀더쉽게탐색할수 있습니다. 

섹션의 헤더와 푸터에 View 를 구성하여 추가적인 정보를 표 시할 수 있습니다. 

Delegate : 동작과 외관을 담당함 / 뷰가 변경되는 사항

DataSource : 데이터를 받아 뷰를 그려줌 / 뷰가 업데이트 되는 사항

 

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

public protocol UITableViewDataSource : NSObjectProtocol {
	
	// 각 섹션에 표시할 행의 개수를 묻는 메서드
	func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) —> Int

	// 특정 인덱스 Row의 Cell에 대한 정보를 넣어 Cell을 반환하는 메서드
	func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

	// 총 섹션 개수를 묻는 메서드
	optional func numberOfSections(in tableView: UITableView) -> Int

	// 특정 섹션의 헤더 타이틀을 묻는 메서드
	optional func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?

	// 특정 섹션의 풋터 타이틀을 묻는 메서드
	optional func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String?

	// 특정 위치의 행을 편집 가능한지 묻는 메서드
	optional func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) ->

	// 특정 위치의 행을 재정렬 할 수 있는지 묻는 메서드
	optional func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) ->

	// 테이블 뷰 섹선 인덱스 타이틀을 묻는 메서드
	optional func sectionIndexTitles(for tableView: UITableView) -> [String]?

	// 인덱스에 해당하는 섹션을 알려주는 메서드
	optional func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int

	// 스와이프 모드, 편집 모드에서 버튼을 선택하면 호출 되는 메서드
	// 해당 메서드에서는 행에 변경사항을 Commit 해야 함
	optional func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath)

	// 행이 다른 위치로 이동되면 어디에서 어디로 이동했는지 알려주는 메서드
	optional func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath)
}

 

 

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

 

public protocol UITableViewDataSource : NSObjectProtocol {
	// 각 섹션에 표시할 행의 개수를 묻는 메서드
	func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int

	// 특정 인덱스 80\의 (6@11에 대한 정보를 넣어 (611 을 반환하는 메서드
	func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

	// 총 섹션 개수를 묻는 메서드
	optional func numberOfSections(in tableView: UITableView) -> Int

	// 특정 섹션의 헤더 타이틀을 묻는 메서드
	optional func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?

	// 특정 섹션의 풋터 타이틀을 묻는 메서드
	optional func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String?

	// 특정 위치의 행이 편집 가능한지 묻는 메서드
	optional func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool

	// 특정 위치의 행을 재정렬 할 수 있는지 묻는 메서드
	optional func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool

	// 테이블 뷰 섹션 인덱스 타이틀을 묻는 메서드
	optional func sectionIndexTitles(for tableView: UITableView) -> [String]?

	// 인덱스에 해당하는 섹션을 알려주는 메서드
	optional func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int

	// 스와이프 모드, 편집 모드에서 버튼을 선택하면 호출 되는 메서드
	// 해당 메서드에서는 행에 변경사항을 (00011 해야 함
	optional func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath)

	// 행이 다른 위치로 이동되면 어디에서 어디로 이동했는지 알려주는 메서드
	optional func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath)
}
반응형
Comments