BountyViewController.swift
//
// BountyViewController.swift
// BountyList
//
//
//
import UIKit
class BountyViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
let viewModel = BountyViewModel()
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetail" {
let vc = segue.destination as? DetailViewController
if let index = sender as? Int{
let bountyInfo = viewModel.bountyInfo(at: index)
vc?.viewModel.update(model: bountyInfo)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return viewModel.numOfBountyInfoList
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? ListCell else {
return UITableViewCell()
}
let bountyInfo = viewModel.bountyInfo(at: indexPath.row)
cell.update(info: bountyInfo)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("\(indexPath.row)번 클릭") // 확인
performSegue(withIdentifier: "showDetail", sender: indexPath.row)
}
}
class ListCell: UITableViewCell {
@IBOutlet weak var imgView: UIImageView!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var bountyLabel: UILabel!
func update(info: BountyInfo) {
imgView.image = info.image
nameLabel.text = info.name
bountyLabel.text = "\(info.bounty)"
}
}
struct BountyInfo {
let name: String
let bounty: Int
var image: UIImage? {
return UIImage(named: "\(name).jpg")
}
init(name: String, bounty: Int) {
self.name = name
self.bounty = bounty
}
}
class BountyViewModel {
let bountyInfoList: [BountyInfo] = [
BountyInfo(name: "브룩", bounty: 83000000),
BountyInfo(name: "쵸파", bounty: 100),
BountyInfo(name: "프랑키", bounty: 94000000),
BountyInfo(name: "루피", bounty: 1500000000),
BountyInfo(name: "나미", bounty: 66000000),
BountyInfo(name: "로빈", bounty: 130000000),
BountyInfo(name: "상디", bounty: 330000000),
BountyInfo(name: "조로", bounty: 320000000),
]
var numOfBountyInfoList: Int {
return bountyInfoList.count
}
func bountyInfo(at index: Int) -> BountyInfo{
return bountyInfoList[index]
}
}
DetailViewController.swift
//
// DetailViewController.swift
// BountyList
//
//
//
import UIKit
class DetailViewController: UIViewController {
@IBOutlet weak var imgView: UIImageView!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var bountyLabel: UILabel!
let viewModel = DetailViewModel()
override func viewDidLoad() {
super.viewDidLoad()
updateUI()
}
func updateUI() {
if let bountyInfo = viewModel.bountyInfo {
imgView.image = bountyInfo.image
nameLabel.text = bountyInfo.name
bountyLabel.text = "\(bountyInfo.bounty)"
}
}
@IBAction func closs(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
}
class DetailViewModel {
var bountyInfo: BountyInfo?
func update(model: BountyInfo?){
bountyInfo = model
}
}
201224 추가
새로 Swift 파일을 만들어 BountyViewController에서 struct BountyInfo 부분을 가져와 좀 더 깔끔하게 코드를 볼 수 있게끔 하였습니다.
( 같이 있어도 무방 )
201225 추가
BountyViewController - BountyViewModel 에
var sortedList, return sortedList[index] 를 추가하였습니다.
빌드하여 확인하면 전과는 다르게 현상금 금액이 큰 순서대로 나열됩니다.