[swift] 네비게이션 뷰에서 목록을 표시하는 방법은?
import UIKit
class YourTableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
let data = ["아이템 1", "아이템 2", "아이템 3"]
// data 배열에 표시할 항목들을 추가합니다.
override func viewDidLoad() {
super.viewDidLoad()
let tableView = UITableView(frame: view.bounds)
tableView.dataSource = self
tableView.delegate = self
view.addSubview(tableView)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
cell.textLabel?.text = data[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// 선택한 항목에 대한 작업을 수행합니다.
print("선택한 항목은 \(data[indexPath.row]) 입니다.")
}
}
이 예제에서는 UITableView
를 만들고, 데이터를 표시하기 위해 UITableViewDataSource
와 UITableViewDelegate
프로토콜을 준수하도록 YourTableViewController를 구현합니다. 이를 통해 목록을 나타내고 사용자의 선택에 반응할 수 있습니다.
더 자세한 내용은 아래의 문서를 참조하세요: