Delegate나 DataSource를 사용하는 UI 컴포넌트

스위프트 상속과 프로토콜 채택

TableView의 DataSource : UITableViewDataSource프로토콜

UITableViewDataSource프로토콜 : 선택적 메서드

TableView의 Delegate: UITableViewDelegate프로토콜

테이블뷰 관련 delegate, datasource 그림 그리기

한 칸 : 로우, 셀
UIViewController vs UIViewTableController


Pin Tool로 Add New Constraints : Table View를 화면 전체를 채움


//
// ViewController.swift
// food
//
// Created by 소프트웨어컴퓨터 on 2025/04/03.
//
import UIKit
var name = ["1한식", "2분식", "3중식", "4일식", "5양식"]
var image = ["1.png", "2.png", "3.png", "5.png", "6.png"]
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var table: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func numberOfSections(in tableView: UITableView) -> Int {
return 6
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell.init(style: .subtitle, reuseIdentifier: "myCell")
cell.textLabel?.text = name[indexPath.row]//"\(indexPath.row)"
cell.detailTextLabel?.text = "\"indexPath.row"
cell.imageView?.image = UIImage(named: image[indexPath.row])
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
table.dataSource = self
table.delegate = self
}
}

Table View Cell을 직접 디자인 : Table View Cell 추가



중요

//
// ViewController.swift
// food
//
// Created by 소프트웨어컴퓨터 on 2025/04/03.
//
import UIKit
var name = ["1한식", "2분식", "3중식", "4일식", "5양식"]
var image = ["1.png", "2.png", "3.png", "5.png", "6.png"]
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var table: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func numberOfSections(in tableView: UITableView) -> Int {
return 6
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(indexPath.description)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// let cell = UITableViewCell.init(style: .subtitle, reuseIdentifier: "myCell")
// cell.textLabel?.text = name[indexPath.row]//"\(indexPath.row)"
// cell.detailTextLabel?.text = "\(indexPath.row)"
// cell.imageView?.image = UIImage(named: image[indexPath.row])
let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) as! MyTableViewCell
cell.myLabel.text = indexPath.description
cell.myImage.image = UIImage(named: image[indexPath.row])
// print(indexPath.description)
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
table.dataSource = self
table.delegate = self
}
}

'iOS프로그래밍 실무' 카테고리의 다른 글
[iOS프로그래밍 실무] 6주차 (0) | 2025.04.10 |
---|---|
[iOS프로그래밍 실무] 4주차 (0) | 2025.03.27 |
[iOS프로그래밍 실무] 3주차 (0) | 2025.03.20 |
[iOS프로그래밍 실무] 2주차 (0) | 2025.03.19 |
[iOS프로그래밍 실무] 1주차 (0) | 2025.03.06 |