Swift
Table View
khon98
2021. 3. 3. 23:05
여러 아이템을 리스트 형태로 보여주고 싶을 때 사용
이미지 사진 이름과 코드에 입력한 이미지 이름이 다르면 이미지 출력이 안됨
// 한 섹션에 Row가 몇개 들어갈 것인가
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10 // 10개의 테이블 row 생성
}
// Cell에 들어갈 데이터를 입력하는 Function
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "FirstCell", for: indexPath)
cell.textLabel?.text = "\(indexPath.row)"
return cell
}
TableView_4
더보기
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
// 두 섹션 앞에 header를 달아서 분리
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
switch section {
case 0:
return "[커피]"
case 1:
return "[Coffee]"
default:
return ""
}
}
// 두 정보를 기본으로 하여 테이블 뷰의 데이터를 컴퓨터가 작성
// 테이블 뷰의 한 섹션당 몇 개의 셀을 담을 것인지를 Return 해줘야 하는 메서드
// 각 섹션에 몇 개의 Cell이 들어가는지를 반환하는 메서드, 인자로 전달되는 section값에 따라 반환값을 달리해주면 됨
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0:
return self.korean.count
case 1:
return self.english.count
default:
return 0
}
}
// 각 row에 해당하는 cell을 Return 해줘야 하는 메서드
// 테이블 뷰에 사용될 모든 Cell들을 반환하는 메서드, 이 메서드의 인자로 IndexPath가 전달, 이 값에서 접근자(.)으로 어떤 section과 row에 들어갈 cell을 원하는지 알 수 있음, 각 Section과 Row에 알맞는 Cell을 반환해주면 됨
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let text: String = indexPath.section == 0 ? self.korean[indexPath.row] : self.english[indexPath.row]
cell.textLabel?.text = text
return cell
}
// 한글, 영어 2개의 섹션을 사용하기 위해, 이에 대한 정보를 전달
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
@IBOutlet weak var tableView: UITableView!
let cellidentifier: String = "cell"
let korean: [String] = ["에스프레소", "아메리카노", "카페라떼"]
let english: [String] = ["espresso", "americano", "cafe latte"]
override func viewDidLoad() {
super.viewDidLoad()
}
}
현상금 리스트 만들기
더보기
Cocoa Touch Class
import UIKit
class BountyViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
let nameList = ["브룩", "쵸파", "프랑키", "몽키 D.루피", "나미", "니코 로빈", "상디", "롤로노아 조로"]
let bountyList = [30000000, 50, 5000000, 500000000, 10000000, 15000000, 200000000, 350000000]
// segue를 수행하기 직전에 수행하는 것에 대해서 준비하는 메서드
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetail" {
let vc = segue.destination as? DetailViewController
if let index = sender as? Int {
vc?.name = nameList[index]
vc?.bounty = bountyList[index]
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
// UITableViewDataSource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return bountyList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? ListCell else {
return UITableViewCell()
}
let img = UIImage(named: "\(nameList[indexPath.row]).jpg")
cell.imgView.image = img
cell.nameLabel.text = nameList[indexPath.row]
cell.bountyLabel.text = "\(bountyList[indexPath.row]) "
return cell
}
// UITableViewDelegate
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("\(indexPath.row)")
performSegue(withIdentifier: "showDetail", sender: indexPath.row)
}
}
// custom cell 생성
class ListCell: UITableViewCell {
@IBOutlet weak var imgView: UIImageView!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var bountyLabel: UILabel!
}
Cocoa Touch Class 2
import UIKit
class DetailViewController: UIViewController {
@IBOutlet weak var imgView: UIImageView!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var bountyLabel: UILabel!
var name: String?
var bounty: Int?
override func viewDidLoad() {
super.viewDidLoad()
updateUI()
}
func updateUI() {
if let name = self.name, let bounty = self.bounty {
let img = UIImage(named: "\(name).jpg")
imgView.image = img
nameLabel.text = name
bountyLabel.text = "\(bounty)"
}
}
// close 버튼을 누르면 창이 닫힘
@IBAction func close(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
}