240228
import UIKit
class MyTableController: UIViewController {
@IBOutlet weak var myTableView: UITableView!
let friendsName: [String] = ["Jenny", "Lisa", "Roze", "Jisoo"]
override func viewDidLoad() {
super.viewDidLoad()
myTableView.backgroundColor = .blue
myTableView.delegate = self
myTableView.dataSource = self
}
}
extension MyTableController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return friendsName.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = myTableView.dequeueReusableCell(withIdentifier: "MyFirstCell", for: indexPath)
cell.textLabel?.text = friendsName[indexPath.row]
return cell
}
}
1. super.viewDidLoad 가 뭐야?
override func viewDidLoad() {
super.viewDidLoad()
}
https://velog.io/@3dots3craters/super.viewDidLoad%EB%A5%BC-%ED%98%B8%EC%B6%9C%ED%95%98%EB%8A%94-%EC%9D%B4%EC%9C%A0%EB%8A%94
위 블로그 글을 읽었는데 여전히 알지 못하겠다
UITableViewDataSource
프로토콜. 즉, 테이블뷰가 해야하는 일을 UITableViewDelegate에게 위임해서 위에 정의한 기능들을 수행하는 것
UITableViewDelegate
테이블 뷰는 데이터를 보여주기만 하는 것이지 자체적으로 데이터를 관리할 수 없기 때문에
UITableViewDataSource 프로토콜을 사용해야 한다.
UITableViewDataSource에서는 의무적으로 선언해줘야 되는 메소드 2개가 있는데 바로
- 몇 개의 셀인지? => numberOfRowsInSection
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return friendsName.count
}
위 코드에선 friendsName 갯수만큼 셀을 만든다는 뜻
let friendsName: [String] = ["Jenny", "Lisa", "Roze", "Jisoo"] --> 배열이 4개이니까 4개의 셀 생성
- 셀이 어떻게 생겼지? -> cellForRowAt
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = myTableView.dequeueReusableCell(withIdentifier: "MyFirstCell", for: indexPath)
cell.textLabel?.text = friendsName[indexPath.row]
return cell
}
dequeueReusableCell(withIdentifier: for: )
지정된 재사용 식별자에 대한 재사용 가능한 테이블 뷰 셀 객체를 반환하고, 이를 테이블에 추가
참고
https://zeddios.tistory.com/55
import UIKit
class MyTableController: UIViewController {
@IBOutlet weak var myTableView: UITableView!
let friendsName: [String] = ["Jenny", "Lisa", "Roze", "Jisoo"]
override func viewDidLoad() {
super.viewDidLoad()
myTableView.backgroundColor = .blue
myTableView.delegate = self
myTableView.dataSource = self
}
}
extension MyTableController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return friendsName.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = myTableView.dequeueReusableCell(withIdentifier: "MyFirstCell", for: indexPath)
cell.textLabel?.text = friendsName[indexPath.row]
return cell
}
}
1. super.viewDidLoad 가 뭐야?
override func viewDidLoad() {
super.viewDidLoad()
}
https://velog.io/@3dots3craters/super.viewDidLoad%EB%A5%BC-%ED%98%B8%EC%B6%9C%ED%95%98%EB%8A%94-%EC%9D%B4%EC%9C%A0%EB%8A%94
위 블로그 글을 읽었는데 여전히 알지 못하겠다
UITableViewDataSource
프로토콜. 즉, 테이블뷰가 해야하는 일을 UITableViewDelegate에게 위임해서 위에 정의한 기능들을 수행하는 것
UITableViewDelegate
테이블 뷰는 데이터를 보여주기만 하는 것이지 자체적으로 데이터를 관리할 수 없기 때문에
UITableViewDataSource 프로토콜을 사용해야 한다.
UITableViewDataSource에서는 의무적으로 선언해줘야 되는 메소드 2개가 있는데 바로
- 몇 개의 셀인지? => numberOfRowsInSection
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return friendsName.count
}
위 코드에선 friendsName 갯수만큼 셀을 만든다는 뜻
let friendsName: [String] = ["Jenny", "Lisa", "Roze", "Jisoo"] --> 배열이 4개이니까 4개의 셀 생성
- 셀이 어떻게 생겼지? -> cellForRowAt
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = myTableView.dequeueReusableCell(withIdentifier: "MyFirstCell", for: indexPath)
cell.textLabel?.text = friendsName[indexPath.row]
return cell
}
dequeueReusableCell(withIdentifier: for: )
지정된 재사용 식별자에 대한 재사용 가능한 테이블 뷰 셀 객체를 반환하고, 이를 테이블에 추가
참고
https://zeddios.tistory.com/55
'iOS Swift > 문법' 카테고리의 다른 글
[Swift] Property observer 프로퍼티 관찰자(옵저버) (0) | 2024.03.11 |
---|---|
[Swift] Initializer (0) | 2024.03.07 |
[Swift] Array (0) | 2024.02.22 |
[Swift] String Comparison & Searching (0) | 2024.02.21 |
[Swift] String Editing #2 (0) | 2024.02.21 |
댓글