0. 첫번째 방법 대로 구현된 상태에서 몇가지만 수정하면됨
[iOS] TableView만드는 첫번째 방법(feat. 임의의 Cell 지정)
[iOS] TableView만드는 첫번째 방법(feat. 임의의 Cell 지정)
1. 스토리보드에 테이블뷰를 생성하고 크기를 정해준다. 2. TableView의 @IBOutlet을 지정해준다. 3. UITableDelegate와 DataSource 프로토콜을 채택해준다. 4. 프로토콜에 준수하는 메서드 두개..
swiftlim.tistory.com
1. TableViewCell을 기존에 만들어놓은 TableView위에 추가한다
2. 값을 넣어줄 label을 TableViewCell위에 추가한다.
3. 중요 TableViewCell의 identifier를 설정한다.
부가적으로 Prototype Cells을 설정하여 label의 개수를 늘리거나 줄일 수 있음

4. identifier를 설정한 이름 그대로 class를 생성 및 TableViewCell의 class를 지정한다.
5. 생성한 class내 @IBOutlet label 변수 추가
6. tableView메서드내 TableViewMain변수에 dequeueResuableCell메서드 추가 및 친자확인
7. didSelectRowAt매개변수가 있는 tableView추가
- 클릭시 이벤트 발생
# 실행화면
# 전체코드
# ViewController
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var TableViewMain: UITableView!
let explain =
"""
1. 어떤 데이터인지 ? -> 전화번호부
2. 테이블의 행 데이터 개수는 몇개인가? -> 100
3. (옵션) 데이터 행을 눌렀을때 이벤트 설정
"""
override func viewDidLoad() {
super.viewDidLoad()
// tableView메서드를 만들었으면 주체를 정해줘야한다.
setTableViewMainEqualSelf()
}
}
// MARK: Method
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// 행의 데이터 개수
return 100
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// 어떤 데이터인지? 그리고 몇번을 반복할 것이냐?
var globalCell = TableViewMethod2.TableViewMain()
// 전과는 다르게 하위 객체로 접근하여 Cell을 재사용하기때문에 친자확인을 거쳐야만 값에 접근할 수 있음
if let cell = TableViewMain.dequeueReusableCell(withIdentifier: "TableViewMain",
for: indexPath) as? TableViewMain {
cell.label.text = "\(indexPath.row)"
globalCell = cell
}
return globalCell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("\(indexPath.row)번째의 행을 클릭!!!")
}
func setTableViewMainEqualSelf() {
// 3. tableView를 만든 주체를 self(뷰컨내 메서드들)로 지정해준다.
TableViewMain.delegate = self
TableViewMain.dataSource = self
}
}
# TableViewMain
import UIKit
class TableViewMain: UITableViewCell {
@IBOutlet weak var label: UILabel!
}
- Reference
- https://www.youtube.com/watch?v=ve8JvQK9r4w
'iOS' 카테고리의 다른 글
[iOS] JSON개념공부 및 사용해보기(feat. Codable, Decoder, Encoder) (0) | 2022.04.10 |
---|---|
[iOS] TableView에 뉴스 제목뿌리기(feat. JSON, News API) (0) | 2022.04.10 |
[iOS] TableView만드는 첫번째 방법(feat. 임의의 Cell 지정) (0) | 2022.04.10 |
[iOS] TableView란? 공식문서를 읽어보자 (0) | 2022.04.10 |
[에러]unrecognized selector sent to instance (0) | 2022.03.25 |