iOS/iOS 앱 개발 올인원 패키지 Online
[iOS] 패스트캠퍼스 챌린지 29일차 - UITableView(2)
듀IT
2021. 10. 4. 12:21
UITableView를 사용해보자.
import UIKit
class BountyViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
// UITableViewDataSource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
return cell
}
//UITableViewDelegate
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("--> \(indexPath.row)")
}
}
먼저 TableView를 이용하려면 UITableViewDataSource, UITableViewDelegate 프로토콜을 채택한다.
그리고 나면 tableView의 테이블뷰의 셀이 몇 개인지, 테이블뷰를 어떻게 보여줄 것인지 UITableViewDataSource를 이용하여 설정한다. numberOfRowsInSection, cellForRowAt을 구현해야한다.
그리고 테이블뷰 셀을 선택했을 때 어떤 반응을 할지 구현하기 위해서는 tableView의 didSelectRowAt을 구현해야한다.
이렇게만 하고 시뮬레이터를 실행하면, 설정한 대로 테이블뷰 셀이 보이지 않는다.
시뮬레이터에서 설정한 테이블뷰 셀을 보이게 하려면 해당 TableView의 DataSource와 Delegate를 담당할 ViewController를 설정해줘야 한다.
패스트캠퍼스 [직장인 실무교육]
프로그래밍, 영상편집, UX/UI, 마케팅, 데이터 분석, 엑셀강의, The RED, 국비지원, 기업교육, 서비스 제공.
fastcampus.co.kr
본 포스팅은 패스트캠퍼스 환급 챌린지 참여를 위해 작성되었습니다.