
@IBOutlet 란?myTableView.backgroundColor = .green 와 같이 데이터를 변경할 수 있다.💡비유하자면 콘센트 선을 연결하는 것과 같다. 연결을 안하면 아무리 전기를 보내도 불이 안켜지는 것과 같다.
@IBOutlet weak var myTableView: UITableView!
UITableViewDelegate, UITableViewDataSource 의 역할UITableViewDelegateUITableViewDataSourcemyTableView.delegate = self
myTableView.dataSource = self
→ self 는 현재 MyTableViewController 가 delegate 와 dataSource 역할을 수행한다는 의미다.
delegate 란?위임 하는 구조이다.delegate 메서드가 호출된다.func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)dataSource 란?이 뷰에 들어갈 데이터는 이걸로 채워줘 라고 알려주는 역할이다.DataSource 에게 묻는다.func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> IntUITableViewDataSource 채택 시 필수 구현 메서드numberOfRowsInSectionfunc tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
tableView : 현재 데이터를 요청하는 테이블 뷰 객체section : 섹션 번호 (기본은 0, 섹션이 여러 개 일때 구분하기 위한 용도)cellForRowAtfunc tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
-> UITableViewCell
UITableViewCell → 화면에 보여줄 셀tableView : 현재 데이터를 요청하는 테이블 뷰 객체indexPath : 섹션 번호 + 줄 번호 정보 (indexPath.section, indexPath.row)💡 포인트 : indexPath.row 를 이용해 cellData 배열에서 데이터를 꺼내오는 것!
let cell = tableView.dequeueReusbleCell(withIdentifier: "myCell", for: indexPath)
cell.textLabel?.text = cellData[indexPath.row]
return cell
→ dequeueReusbleCell : 재사용 가능한 셀을 가져오기. (메모리의 효율성 증가)
UITableViewDelegate 를 채택할 때 필수 구현은 아닌 메서드didSelectRowAtfunc tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
viewDidLoad() 에서 delegate, dataSource 를 연결한다.dataSource 호출numberOfRowsInSection() 호출 → 테이블뷰에 몇 줄을 그릴거야?cellForRowAt() → 각 줄에 뭘 그릴거야?delegate 메서드 실행| 용어 | 설명 |
|---|---|
| @IBOutlet | 스토리보드 ↔ 코드 연결 |
| delegate | 테이블뷰 이벤트 처리자 (행동) |
| dataSource | 테이블뷰 데이터 제공자 (데이터) |
| indexPath.row | 현재 셀의 줄 번호 |
| indexPath.section | 현재 셀의 섹션 번호 |
| dequeueReusableCell | 재사용 셀 꺼내기 (메모리 관리) |