daydr3am3r
daydr3am3r

Reputation: 954

SWIFT - table.reloadData() not / stopped working

I am trying to refresh a tableView using table.reloadData() in SWIFT 3. I managed to get this working some time ago but now it simply stopped working out of the blue. No errors, no warning, nothing.

override func viewWillAppear(_ animated: Bool)
{
    print("MainVC > viewWillAppear")

    super.viewWillAppear(animated) 
    initializeCollectionView()
}

func initializeCollectionView()
{
    print("MainVC > initializeCollectionView")

    projectsArray = []
    getDocumentDirectoryContent()

    if(projectsArray.count > 0)
    {
        print(projectsArray.count)

        //For testing only - To make sure the array is not empty
        for var i in 0..<projectsArray.count
        {
            print(projectsArray[i].project_name)
        }

        cvProjects.isHidden = false
        ivMainVCPlaceholder.isHidden = true
    }
    else
    {
        cvProjects.isHidden = true
        ivMainVCPlaceholder.isHidden = false

        cvProjects.reloadData()
    }
}

Every time the app starts it calls the it grabs the data from documentDirectory and fills in the projectsArray array with objects. This works just fine.

The initializeCollectionView is called in viewWillAppear because the tableView was also supposed to be refreshed when a child viewController is dismissed. It is also called on tap:

@IBAction func btnRefreshCollectionAction(_ sender: Any)
{
    print("MainVC > btnRefreshCollectionAction")

    initializeCollectionView()
}

The problem is now the table won't refresh neither when a child viewController is dismissed nor when I tap on btnRefreshCollectionAction.

However, the projectsArray array is not empty in initializeCollectionView.

My first thought was to blame the Xcode upgrade I performed right before I noticed this stopped working but usually if there is any problem after an upgrade there are warnings and/or errors.

Any ideas?

Upvotes: 1

Views: 124

Answers (1)

Sivajee Battina
Sivajee Battina

Reputation: 4174

That is because you are calling reload method only when the array is empty. When you have objects in an array you are executing some other code but not reload. Either write reload method in viewWillAppear or else put reload method in If condition also. That should work.

Upvotes: 1

Related Questions