Я пытаюсь создать простое приложение, в котором я могу добавить ячейку класса в TableView, но она не отображается, когда я нажимаю кнопку Добавить. Я не уверен, в какой части я делаю неправильно.
Вот мой Main.storyboard
Вот мой код в ManageViewController.swift
import UIKit
class ManageViewController : UIViewController, UITableViewDataSource{
@IBOutlet weak var tableView: UITableView!
var classList = [String]()
override func viewDidLoad() {
super.viewDidLoad()
//classList = ["CSC 2431","CSC 4498"]
createAlert(title: "Add class", message: "Click plus button to add class")
classList = ["CSC 2431","CSC 4898"]
// Do any additional setup after loading the view.
}
@IBAction func onAddTapped(_ sender: UIBarButtonItem) {
let alert = UIAlertController(title: "Add Class", message: nil, preferredStyle: .alert)
alert.addTextField { (classListTF) in
classListTF.placeholder = "Enter Class"
}
let action = UIAlertAction(title: "Add", style: .default) { (_) in
guard let list = alert.textFields?.first?.text else { return }
print(list)
self.classList.append(list)
self.tableView.reloadData()
}
alert.addAction(action)
present(alert, animated: true)
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return (classList.count)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "classCell")
cell.textLabel?.text = classList[indexPath.row]
return(cell)
}
}