Tableview reloadData() doesn't work

Can anyone help me? I'm using a tableView and trying to fill it with values entered by user using a Bar Button item, but whenever I click on save alert action, self.tableView.reloadData() doesn't work !

The code is as below:


import UIKit
class ViewController: UIViewController {
    var names:[String]=[]
    @IBOutlet weak var tableView: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title="The List"
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
    }
   
    @IBAction func addName(_ sender: UIBarButtonItem) {
        let alert = UIAlertController(title: "New Name",
                                      message: "Add a new name",
                                      preferredStyle: .alert)
       
        let saveAction = UIAlertAction(title: "Save",
                                       style: .default) {
                                        [unowned self] action in
                                       
                                        guard let textField = alert.textFields?.first,
                                            let nameToSave = textField.text else {
                                                return
                                        }
                                       
                                        self.names.append(nameToSave)
                                        self.tableView.reloadData()
        }
       
        let cancelAction = UIAlertAction(title: "Cancel",
                                         style: .default)
       
        alert.addTextField()
       
        alert.addAction(saveAction)
        alert.addAction(cancelAction)
       
        present(alert, animated: true)
       
    }
}
extension ViewController:UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return names.count
    }
    func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath)-> UITableViewCell {
           
            let cell = tableView.dequeueReusableCell(withIdentifier: "Cell",for: indexPath)
            cell.textLabel?.text = names[indexPath.row]
            return cell
    }
}

Add `tableView.dataSource = self` to the end of `viewDidLoad()`

Doesn't work : do you mean you see no change in the tableView. What do you get in tableView ?

If you ask a second add, does the first added name finally show ?

Are you sure reload is called ?


I would do 2 tests:

- in let saveAction, add a print to check the array, after appending

self.names.append(nameToSave)
print(names)

That will also check that reloadData is called


- in tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath)-> UITableViewCell : just before returning

print(cell.textLabel!.text)

I used control+drag in stroyboard and also even in code

but reload never called

thanks for reply

The first test passed successfully;

But the second failed, This means reload never called.Thanks

Probably, you return from the guard.


Add some test here :

    if alert.textFields?.first == nil {
     print("alert.textFields?.first is nil")
   } else {
        if alert.textFields?.first!.textFields.text == nil {
           print("textFields.text is nil")
       }
  }
guard let textField = alert.textFields?.first,  let nameToSave = textField.text else { 
    return
   }
Tableview reloadData() doesn't work
 
 
Q