




class TableViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
// 각 섹션별 데이터
private let sections: [(header: String?, items: [(imageName: String, title: String)])] = [
(nil, [
("wifi", "Wi-Fi"),
("bolt.horizontal", "Bluetooth"),
("airplane", "Airplane Mode"),
("battery.100", "Battery")
]),
("General", [
("gear", "General"),
("magnifyingglass", "Search")
]),
("Other", [
("person.crop.circle", "Profile"),
("lock", "Privacy")
])
]
override func viewDidLoad() {
super.viewDidLoad()
setNavigationTitle()
tableView.delegate = self
tableView.dataSource = self
}
//navigation title 세팅
func setNavigationTitle() {
title = "Table View"
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.largeTitleDisplayMode = .automatic
}
}
extension TableViewController: UITableViewDelegate, UITableViewDataSource {
// 섹션 개수
func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
// 각 섹션별 행 개수
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sections[section].items.count
}
// 셀 설정
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") ??
UITableViewCell(style: .default, reuseIdentifier: "Cell")
let cellData = sections[indexPath.section].items[indexPath.row]
cell.imageView?.image = UIImage(systemName: cellData.imageName)
cell.textLabel?.text = cellData.title
cell.accessoryType = .disclosureIndicator
return cell
}
// 섹션 헤더 설정
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sections[section].header
}
// 셀 선택 시 동작
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
print("\(tableCellData[indexPath.row].title) 선택됨")
}
}
테이블 뷰의 데이터 관리
테이블 뷰의 동작 및 UI 관리
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
switch indexPath.section {
case 0:
switch indexPath.row {
case 0:
if let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "WifiViewController") as? WifiViewController {
self.navigationController?.pushViewController(viewController, animated: true)
}
default: break
}
default: break
}
}
class WifiViewController: UIViewController {
@IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
title = "Wifi"
label.text = "WifiViewController"
navigationController?.navigationBar.prefersLargeTitles = false
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationController?.navigationBar.prefersLargeTitles = true
}
}
TableViewController(현재 MVVM 패턴으로 변환)
https://github.com/zongbeen/AboutiOS/blob/main/AboutiOS/VIew/TableViewController.swift
