📱 모바일 프로그래밍/iOS 프로그래밍 실무
iOS 프로그래밍 실무 5주차
SA성아
2025. 4. 2. 16:49
테이블뷰 - 시험에 가장 많이 나옴
프로토콜(protocol)과 Delegate

table view - 두 개의 프로토콜 사용
부모 - 상속(없거나 하나만), 프로토콜 - 채택(여러 개)

나머지는 선택적으로 사용한다 .
UITableViewDataSource프로토콜 : 선택적 메서드
- numberOfSections(in:) - 테이블 뷰에 섹션 수 지정
- tableView(_:titleForHeaderInSection:) - 각 섹션의 헤더에 표시될 텍스트 지정
- tableView(_:titleForFooterInSection:) - 각 섹션의 푸터에 표시될 텍스트 지정
- tableView(_:canEditRowAt:) - 셀을 삭제하거나 추가할 때
- tableView(_:canMoveRowAt:) - 셀의 순서를 변경할 때
TableView의 Delegate: UITableViewDelegate프로토콜
- 특정한 때에 자동으로 일어난다.
테이블뷰(TableView): 항목을 표로 보여줘요

라이브러리 - 갖다 쓰는 거
프레임워크 - 만들어진 틀에 기능을 추가하면 알아서 동작하는 것

- table view controller를 쓰면 table view와 table cell이 모두 들어있어서 편하다.

- 화면 가득 채우기


섹션: table view가 몇개의 통으로 이루어져있는지
row: 몇 칸이냐

import UIKit // UI로 시작하는 애들은 다 UIKit에 들어가있음
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var table: UITableView!
func numberOfSections(in tableView: UITableView) -> Int {
return 3
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell.init(style: .default, reuseIdentifier: "myCell")
cell.textLabel?.text = indexPath.description
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
table.delegate = self
table.dataSource = self
}
}
- indexPath.description: 앞에는 Section 번호 뒤는 Row 번호
import UIKit // UI로 시작하는 애들은 다 UIKit에 들어가있음
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var table: UITableView!
func numberOfSections(in tableView: UITableView) -> Int {
return 3
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell.init(style: .subtitle, reuseIdentifier: "myCell")
cell.textLabel?.text = "\(indexPath.row)"
cell.detailTextLabel?.text = indexPath.description
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
table.delegate = self
table.dataSource = self
}
}

Free Icons and Stickers - Millions of resources to download
Download Free Icons and Stickers for your projects. Resources made by and for designers. PNG, SVG, EPS, PSD and CSS formats
www.flaticon.com
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell.init(style: .subtitle, reuseIdentifier: "myCell")
cell.textLabel?.text = "\(indexPath.row)"
cell.detailTextLabel?.text = indexPath.description
cell.imageView?.image = UIImage(named: "happy.png")
return cell
}
- 이미지 넣기

import UIKit // UI로 시작하는 애들은 다 UIKit에 들어가있음
var image = ["1.png", "2.png", "3.png", "4.png", "5.png"]
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var table: UITableView!
func numberOfSections(in tableView: UITableView) -> Int {
return 6
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell.init(style: .subtitle, reuseIdentifier: "myCell")
cell.textLabel?.text = "\(indexPath.row)"
cell.detailTextLabel?.text = indexPath.description
cell.imageView?.image = UIImage(named: image[indexPath.row]) // 배열이름[indexPath.row]
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
table.delegate = self
table.dataSource = self
}
}
- 이미지 5개 받아서 각 셀에 5개 이미지 출력하기

let cell = UITableViewCell.init(style: .subtitle, reuseIdentifier: "myCell")
- .init 없어도 됨

constraints 확인

return형 UITableViewCell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) as! MyTableViewCell // 다운캐스팅
cell.myLabel.text = foodName[indexPath.row]
print(indexPath.description)
return cell
}
- cellForRowAt: UITableViewDataSource 프로토콜에 포함된 메서드로, UITableView의 각 행(셀) 을 생성하고 반환하는 역할
즉, UITableView에서 특정 인덱스의 셀을 화면에 표시할 때 어떤 셀을 어떻게 구성할지를 정하는 함수
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(indexPath.description)
}
- tableview에서 cell을 눌렀을 때 자동으로 호출
출처: iOS 프로그래밍 실무 강의 자료