import UIKit
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
var array = [String]()
var lastId = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
array = UserDefaults.standard.array(forKey: "aa") as? [String] ?? [] // 저장된 array값 불러오기 key: "aa"
lastId = UserDefaults.standard.integer(forKey: "bb") // 저장된 lastId 불러오기 key: "bb"
}
@IBAction func btnAdd(_ sender: Any) {
let nextId = lastId + 1
array.append(String(nextId))
lastId = nextId
tableView.reloadData()
}
@IBAction func btnSave(_ sender: Any) { // save버튼 누를시
UserDefaults.standard.set(array, forKey: "aa") // array값 저장 key: "aa"
UserDefaults.standard.set(lastId, forKey: "bb") // lastId 저장 key: "bb"
}
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? tableViewCell else {
return UITableViewCell()
}
cell.lbl.text = array[indexPath.row]
return cell
}
}
class tableViewCell: UITableViewCell {
@IBOutlet weak var lbl: UILabel!
}
'ios 개발' 카테고리의 다른 글
[iOS] Delegate를 이용하여 뷰컨트롤러간 데이터 전송하기 (0) | 2021.09.05 |
---|---|
[iOS] Closure를 이용하여 뷰컨트롤러간 데이터 전송하기 (0) | 2021.09.05 |
[ios] pageControl로 tableView 제어하기 (0) | 2021.08.07 |
[ios]textfield 터치시 DatePicker 나오게 하기 (0) | 2021.08.06 |
[ios] 테이블뷰 기초 (0) | 2021.08.05 |