UITableView not showing new cell data - uitableview

The first time my UITableView loads, my cellForRowAtIndexPath method gets correctly called. The subsequent times I show my IUTableView, this method is not called and the old data is shown.
Neither are the viewWillAppear method nor the numberOfRowsInSelection called.
Anyone know what could be wrong?
Thanks
Deshawn

When you get your new data, fire -reloadData on the table view. It'll run all its data source methods to get a fresh look at its source data.
Don't tie that reload on the display of the view. Tie it to when you receive the new data.

Related

TableView rows not updating after core data insert

I'm very new to swift programming. I have been playing around with this for a while, but I am not getting anywhere so asking here.
I have a tableview which I can load the data into the view from CoreData no problem. I have an ADD button at the top that segue's to a new tableview, with a long list of options they can pick from. This tableview also works fine, and includes a search bar.
When the user selects an item row from the second tableview, it inserts that item into CoreData and segue's back to the first tableview. This is where my problem is, the data does NOT update on the visible view.
I call tableview.reloaddata() and I can see my code calling the fetchedResultsController with the new query that would return with the new data. But the code never gets to the cellForRowAtIndexPath func so therefore the visible data view never changes. It remains the same display that was visible when the add button was pressed.
How does the visible data get updated? What am I missing?
When using an NSFetchedResultsController, if you want it to "automatically" update the contents of the tableview then there are a couple of things you need to make sure of...
You have to become the NSFetchedResultsControllerDelegate and implements the methods necessary for the updating of the table view. These are quite lengthy and can be found on the Ray Wenderlich website. The code on here is in Objective-C but it's fairly easy to convert to Swift.
The second thing you need is to make sure that the core data update is done on a background thread. Again the website linked above shows this.
Once you've done that then you don't actually need to run [tableview reloadData] because the fetched results controller methods will manage everything for you.

UITableView datasource / reload race condition

Issue:
I have a UITableView that is reloaded by a controller as soon as it receives an NSNotification. The data structure that the cellForRowAtIndexPath uses as the datasource may change while the table is refreshing.
Background:
Whenever the app's data model changes a NSNotification gets fired and my UITableViewController who handles the datasource of the UITableView gets notified to execute a "refreshReload" method. The "refreshReload" method retrieves the new data from the Model and then asks for [tableView reloadData]. This is classes MVC pattern where the model gets changed, the controller gets notified and the view gets updated. App crashes when there are quick Notifications back to back that change the data quickly. I feel that while the table is calling cellForRowAtIndexPath: the data structure that contains the data changes during the execution.
What would be a good pattern to follow to avoid this, is there a way to stop reloading of a table so that I can first perform the stop then change the datasource ?
If I understand your question correctly, I would use global flag to lock data changes.
e.g. use singleton pattern to hold your flag value. Check whether flag is locked or not. If flag is locked, do not call or disable data change methods. When UITableView is updating, lock flag until updating is finished.
The crash will be come if the data change very frequently i.e. at the time table is updating and table get another reload call.
To resolve crashes in your app:
First thing you can do is that avoid that much frequent calling of reload table. Maintain a Flag (in Notification observer method that you set for receiving notification) ,that flag tells that table need to reload now when user come to this tableview screen you create a timer that will call a method after (say) every one minute . In that method, check for flag , if it is YES then reload the table and Change Flag value to NO else there is nothing you need to do.
I hope that will resolve your problem for sure.

numberOfSectionsInTableView is requested in/after viewDidLoad ONLY if tableview is empty

A simple tableviewController, empty. A modal that can be launched from a button. No data in the data source for the tableview, and no rows displayed.
I open the modal, use it to add an item, and return to the tableview controller. The tableview updates itself automatically, and the new row is displayed.
I add a second item. The table view does NOT update automatically.
I can tell by logging inside numberOfSectionsInTableView that even if I go to add the first item and cancel, the tableview refreshes - it asks for the number of sections, rows, and the cell to display (if there is one). But not if there is one pre-existing row in the table.
I've scoured my code but can't find anything that would cause the tableview to know when the first item is added, but none afterwards.
Any ideas?
EDIT - I have further narrowed my issue so will close this shortly and have posted a more specific question at Why does an empty tableView check the number of sections but a non-empty one does not?
from Apple's documentation:
UITableView overrides the layoutSubviews method of UIView so that it
calls reloadData only when you create a new instance of UITableView or
when you assign a new data source.
maybe you are setting data source of table view after showing that modal? So at the second time data source does not changes and tableView does not update.
another key (I'm not sure about that) may be the showing of the modal first time. The method -addSubview for modal is causing -layoutSubviews.

UITableView reloadData fires only once

I have a TabBarController with two tabs SCHEDULE and DATE RANGE
When the view loads I'm hitting a webservice to collect the end users Schedule for that day. After parsing the XML I call ScheduleTable reloadData and all is well. I have tons of NSLog statements to follow the performance.
Clicking the DATE RANGE tab displays a DatePicker. After selecting the date range I have a button which when clicked passes the dates to the webservice again. In my log I can see the new dataset and can verify I have the data I need, when the code reaches the reloadData line, it does not fire the UITableView reload methods.
Any help is appreciated. I've tried viewWillAppear and self.ScheduleTable reloadData, neither have helped.
Check to see if you've connected the UITableView to the ScheduleTable IBOutlet in Interface Builder.
You say that the first refresh is working. That may be because it is automatically done by the tableview.
I understand that the second time you are calling reloadData programatically. If you haven't connected the IBOutlet, then your ScheduleTable variable is nil and [ScheduleTable reloadData] will do nothing.
when the code reaches the reloadData line, it does not fire the UITableView reload methods. Any help is appreciated. I've tried viewWillAppear and self.ScheduleTable reloadData
Just before saying [self.scheduleTable reloadData] (never start an ordinary variable name with a capital letter), log the table:
NSLog(#"%#", self.scheduleTable);
I'm betting it won't be the table and that you're accidentally sending this message into empty air.

Updating a UITableView's data

In my application, I have a UITableView with a few rows (let's call it TV1), now, at the bottom of these rows is a row that drills down into another (TV2). This new TV2 asks you which type of data you would like to add to the first view. Then, when the user selects which kind, they're brought to another view, in which they fill in some fields, the data is saved, and they're sent back to TV1, however, the data they entered, isn't loaded and I'm not really sure how to do this. Any ideas?
Maybe you did this already, but how about using [TV1 reloadData];? This forces the entire table to reload, yet there are also more specific methods for reloading just the cells that have been altered. See also the documentation: UITableView
Reload your table view by calling [self.tableView reloadData] in the viewcontroller's viewWillAppear: method. It will be called when the user switches back to your view. Right before it is actually put on screen.

Resources