import UIKit
class ViewController: UIViewController {
var items = [String]()
@IBOutlet weak var table: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
table.delegate = self
table.dataSource = self
}
@IBAction func addBtnPressed(_ sender: UIBarButtonItem) {
var textField = UITextField()
let alert = UIAlertController(title: "Add new item", message: "", preferredStyle: .alert)
let cancel = UIAlertAction(title: "cancel", style: .default) { (cancel) in
}
let save = UIAlertAction(title: "save", style: .default) { (save) in
self.items.append(textField.text!)
self.table.reloadData()
}
alert.addTextField { (text) in
textField = text
textField.placeholder = "Add new item"
}
alert.addAction(cancel)
alert.addAction(save)
self.present(alert, animated: true, completion: nil)
}
}
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = items[indexPath.row]
return cell
}
}