1

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?

4
  • 1
    Well you're only calling reloadData when projectsArray.count is 0. Commented Apr 10, 2017 at 14:59
  • 1
    So this might be a crude observation, but shouldn't you also be reloading the data if the count of the objects you fetched > 0? Because as I can see from the code you supplied, if the array is not empty, you don't reload! Commented Apr 10, 2017 at 15:00
  • Now I feel dumb :| And I've been staring at it for some time. Thanks guys :D Commented Apr 10, 2017 at 15:03
  • A good indication would have to check numberOfSections(in tableView:) and/or tableView(_ tableView:numberOfRowsInSection:) ;) Commented Apr 10, 2017 at 15:13

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.