Search code examples
xcodeuitabbarcontrolleruitableviewnsfilemanagerreloaddata

UITableView reloadData not updating UI, only after restart


Running into a strange problem:

I have a TableViewController (call it recent tab) that is one of the tabs of a UITabBarController. I have items getting saved into sandbox (documents folder) in the first tab bar (browse tab). I am then pulling them out in the second tab bar (recent) and setting them up in cellForRowAtIndexPath.

Here's the strange behavior: while I'm in first view controller (browse) adding items to the sandbox, everything works fine. I open the second tab (recent) that is the TableViewController and I see the items appear there. Then I try to go back to the first controller (browse) to add more items and they do not appear (when I go back to recent).

However, when I relaunch the application, the items I selected the second time do show up (in recent)!

I have tried to call tableView reloadData in recent (practically everywhere I could). I have tried to call [tableView beginUpdates].

No change, table view (for recent) updates properly only after restart of application.

Any suggestions?


Solution

  • I believe the issue was that the CellForRowAtIndexPath did not have an array with indexes to pull items out of, therefore causing tableView to have issues calculating how many rows there are (read this in another SO thread).

    The array has to already exist before CellForRowAtIndexPath is called, not be instantiated inside of it (could be passed to the class through a public api or instantiated in viewDidLoad for ex). Otherwise it will be instantiated every time CellForRowAtIndexPath is called and there will be no static index that could be used to create the cells.