레이아웃 설정
스토리보드를 통해 ViewController에 TableView를 추가하고,
TableViewCell에 ImageView, Label, Switch를 넣었다.
cell의 identifier를 "myCell"로 설정했다.
테이블뷰를 해당 ViewController파일에 outlet으로 연결하고,
TableViewCell 파일을 만들고 cell의 커스텀클래스로 지정해준 뒤
cell안의 요소들을 outlet으로 연결해주었다.
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
let img = ["heart", "heart.fill", "heart.circle", "heart.circle.fill"]
let label = ["1", "2", "3", "4"]
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
}
extension ViewController: UITableViewDelegate {
}
extension ViewController: UITableViewDataSource {
// 행의 개수
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return img.count
}
// 등록했던 cell identifier인 "myCell"을 재사용한다.
// as! 타입캐스팅으로 TableViewCell 타입으로 다운캐스팅 후 아울렛 변수에 접근한다.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) as! TableViewCell
cell.myImage.image = UIImage(systemName: img[indexPath.row])
cell.label.text = label[indexPath.row]
return cell
}
}
import UIKit
class NextViewController: UIViewController {
@IBOutlet weak var myImage2: UIImageView!
@IBOutlet weak var label2: UILabel!
var nextImage: String?
var nextLabel: String?
override func viewDidLoad() {
super.viewDidLoad()
myImage2.image = UIImage(systemName: nextImage!)
label2.text = nextLabel!
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "mySegue"{
if let destination = segue.destination as?
NextViewController {
if let selectdeIndex =
self.tableView.indexPathForSelectedRow?.row {
destination.nextImage=img[selectdeIndex]
destination.nextLabel=label[selectdeIndex]
}
}
}
}
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
let img = ["heart", "heart.fill", "heart.circle", "heart.circle.fill"]
let label = ["1", "2", "3", "4"]
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "mySegue"{
if let destination = segue.destination as?
NextViewController {
if let selectdeIndex =
self.tableView.indexPathForSelectedRow?.row {
destination.nextImage=img[selectdeIndex]
destination.nextLabel=label[selectdeIndex]
}
}
}
}
}
extension ViewController: UITableViewDataSource {
// 행의 개수
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return img.count
}
// 등록했던 cell identifier인 "myCell"을 재사용한다.
// as! 타입캐스팅으로 TableViewCell 타입으로 다운캐스팅 후 아울렛 변수에 접근한다.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) as! TableViewCell
cell.myImage.image = UIImage(systemName: img[indexPath.row])
cell.label.text = label[indexPath.row]
return cell
}
}
import UIKit
class NextViewController: UIViewController {
@IBOutlet weak var myImage2: UIImageView!
@IBOutlet weak var label2: UILabel!
var nextImage: String?
var nextLabel: String?
override func viewDidLoad() {
super.viewDidLoad()
myImage2.image = UIImage(systemName: nextImage!)
label2.text = nextLabel!
}
}
import UIKit
class TableViewCell: UITableViewCell {
@IBOutlet weak var myImage: UIImageView!
@IBOutlet weak var label: UILabel!
@IBOutlet weak var mySwitch: UISwitch!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}