Search code examples
iosuitableviewswiftios7uialertview

Swift: The tableView do not reload data after press button in alert view


I have add the alert view which let user to control that they can delete the all items in their database, but the table do not reload the data again.

I have tried self.tableView.delegate = self and self.tableView.reloadData(), but it does not work.

Here is the code:

class ItineraryViewontroller: UITableViewController {
....

         func clearAll(){

           var clearAlert = UIAlertView()
            clearAlert.delegate = self
            clearAlert.title = "Clear All"
            clearAlert.message = "Do you want to delete ALL events in itinerary?"
            clearAlert.addButtonWithTitle("OK")
            clearAlert.addButtonWithTitle("Cancel")
            clearAlert.show()

        }



        func alertView(View: UIAlertView!, clickedButtonAtIndex buttonIndex: Int){
            switch buttonIndex{

            case 0:
                NSLog("OK");

                 self.db.open()
                 self.db.executeUpdate("DELETE FROM bookmark",withArgumentsInArray: nil)

                 //self.tableView.delegate = self
                 //self.tableView.reloadData()
                 let selfClass =  ItineraryViewontroller()
                 selfClass.tableView.reloadData()


                break;
            case 1:
                NSLog("Cancel");
                break;
            default:
                NSLog("Default");
                break;
                //Some code here..

            }
        }
...
}

Solution

  •     let selfClass =  ItineraryViewontroller()
         selfClass.tableView.reloadData()
    

    These lines are not correct. You create a new instance of ItineraryViewontroller.

    You should get the references of ItineraryViewontroller that is used to show your tableview.

    Meanwhile,you have to update your dataSource of tableview