데이터를 관리하고 테이블 뷰에 셀을 제공하기 위해 객체가 채택하는 메소드
@MainActor protocol UITableViewDataSource
테이블뷰는 데이터를 띄우는 것만 관리하며, 데이터 자체는 관리하지 않습니다. 데이터를 관리하기 위해 테이블에 데이터 소스 객체, 즉 UITableViewDataSource
프로토콜을 구현하는 객체를 제공합니다. 데이터 소스 객체는 테이블의 데이터 관련 요청에 응답합니다. 또한 테이블의 데이터를 직접 관리하거나 앱의 다른 부분과 조정하여 해당 데이터를 관리합니다. 데이터 소스 객체의 책임은 다음과 같습니다.
오직 두 가지 메소드만 필요하며(required), 다음 예제 코드에 나와 있습니다.
// 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
}
테이블에 특정 기능을 활성화하려면 이 프로토콜의 다른 메서드를 사용하세요. 예를 들어 행에 대한 스와이프-삭제
기능을 수행하려면 tableView(_:commit:forRowAt:)
메서드를 구현해야 합니다.
데이터 소스 객체를 사용하여 테이블의 셀을 만들고 구성하는 방법에 대한 자세한 내용은 Filling a Table with Data를 참조하세요.
테이블뷰는 NSIndexPath
객체의 행 및 섹션 프로퍼티를 사용하여 셀의 위치를 사용자에게 전달합니다. 행 및 섹션 인덱스는 0 기반이므로, 첫 번째 섹션은 인덱스 0에 있고 두 번째 섹션은 인덱스 1에 있습니다. 마찬가지로 각 섹션의 첫 번째 행은 인덱스 0에 있으므로 행을 고유하게 식별하려면 섹션 값과 행 값이 모두 필요합니다. 테이블에 섹션이 없는 경우, 행 값만 있으면 됩니다.
func tableView(UITableView, numberOfRowsInSection: Int) -> Int
func numberOfSections(in: UITableView) -> Int
func tableView(UITableView, cellForRowAt: IndexPath) -> UITableViewCell
func tableView(UITableView, titleForHeaderInSection: Int) -> String?
func tableView(UITableView, titleForFooterInSection: Int) -> String?
func tableView(UITableView, commit: UITableViewCell.EditingStyle, forRowAt: IndexPath)
func tableView(UITableView, canEditRowAt: IndexPath) -> Bool
func tableView(UITableView, canMoveRowAt: IndexPath) -> Bool
func tableView(UITableView, moveRowAt: IndexPath, to: IndexPath)
func sectionIndexTitles(for: UITableView) -> [String]?
func tableView(UITableView, sectionForSectionIndexTitle: String, at: Int) -> Int
NSObjectProtocol
UITableViewController
,UITableViewDiffableDataSource
, UITableViewDiffableDataSourceReference