UITableView does not repopulate with reloadData - uitableview

I have a UITableView that successfully populates with a NSMutableArray. However, when I try to use a search function that I have implemented, the UITableView does not repopulate at all.
The NSMutableArray also contains the correct number of objects according to the search but when realoadData is called, the numberOfSections and numberOfRows methods are called, but the cellForRow method is not (even though the search returns 1 item). The delegate and datasource for the UITableView are correctly set up and connected using IB.
I am happy to paste some code, but I am unsure as to what relevant code would be helpful in discussion here.
Thanks

Answer: Just me being stupid and using a saved NSArray rather than the updated NSMutableArray to populate the table. Silly me...

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.

When is tableView:numberOfRowsInSection: called in UITableView?

tableView:numberOfRowsInSection is sent to the delegate of a UITableView to find out how many rows it needs to have in a given section.
My question is, when and how often is this method called?
The method is called very first time the tableview is getting loaded and if you are more interested in the delegates then put a breakpoint and check when and where which delegate is called and how many times.
Below are the instances when that function will get called,
For the first time when table is loaded
the time you reload the table data
the time you add/update/delete your row or sections dynamically.
The method - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section is a protocol method of the UITableViewDataSource - protocol. It will be called
the very first time your table view is loaded based on that you have set the dataSource properly, e.g.
self.yourTableView.dataSource = self;
If you are interested in updating your table again at a later time you can call
[self.yourTableView reloadData];
in order to reload the entire table. If you are only interested in reloading a part of your table you can do something similar to
NSIndexSet *reloadSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [self numberOfSectionsInTableView:self.yourTableView])];
[self.yourTableView reloadSections:reloadSet withRowAnimation:UITableViewRowAnimationAutomatic];
Hope it helps!
My question is, when and how often is this method called?
Short Answer : When your UITableView needs to update something.
Long Answer : Delegates Methods generally called themselves however it may be called multiple times when your UITableView needs to update something. By default, it's called very first time the tableview is getting loaded or updated (reloaded).
It depends on how often user will scroll UITable view to section and how many sections there are. This value, which is returned by this function and is casched. Method will need be revoked if you will update content of table view (filtering results, or updating data via reloadData).
Best thing for you will be to add logging to this function and check this yourself.

NSMutableDictionary updates not working in UItableview

I'm using *NSMutableDictionary articleDictionary as a datasource for tableview.I have different tab in tableview page, each tab i'm using different xml file [https://domain.com/api/categories/3/user/xml ] . I received different set of values in (void)viewDidUnload area but not reflect in table view .Tableview still showing old values.Please help me . Thanks
self.articleDictionary = [NSDictionary dictionaryWithContentsOfURL: [NSURL URLWithString:urlString]];
Try triggering reload of the table view when you are done with parsing. That is reload the table from the same method where you update your NSMutableDictionary.
Call [self.tableView reloadData];.
You have to tell the tableview to update. The tableview can't guess that your value has been updated ;)
Something like that should help
// Update your files normally...
// ...
// Trigger the reload
[self.tableView reloadData];
You can put ample debugging stuff into your table view controller class.
Here are some pointers:
When you call reloaddata, do you observe how many times tableView:cellForRowAtIndexPath get called? Do you get proper index values there?
Have you updated your tableView:numberOfRowsInSection: to reflect change in data source? ie the latest NSMutableDictionary count?
In your tableView:cellForRowAtIndexPath, put NSLog about NSMutableDictionary to see if it reflects the latest status?
I resolved the issue.
I changed my UiviewController superclass to UITableViewController. Thanks for your helps.

reloadData seems to work but doesn't show any cell on asynchronous search on tableView

I've been stuck on this for all day.
I have a Nib file with a custom table cell wich I use to load into a
tableView instance variable of a view controller.
In the view controller I have a Search Bar and a Table View IBOutlets and a NSMutableArray to store my data as instance variables, among other stuff.
I have an object wich does asynchronous calls to an external API using Restkit so I define my view controller as it's delegate to receive the data when the request is finishe, save it on the NSMutableArray and then trigger a reloadData.
Because I'm making asynchronous calls the method shouldReloadTableForSearchString is in charge of sending the request and then return NO.
If I return YES instead of NO, making a call to reloadData after, my cells get loaded into the tableView one step back of my calls, that means that if I type the first letter nothing happens but on the second letter I get the results from the first letter. That behavior it's ok because the instance variable are setted after shouldReloadTableForSearchString returning YES. That is expected, Ok.
The problem that I'm facing is that if I use only reloadData, with shouldReloadTableForSearchString returning NO, the cells are not being assigned to the tableView. I checkhed and cellForRowAtIndexPath is being called and every cell is created with the data but it does not show.
Maybe I have problems with the datasource property, but I'm not sure.
I don't get the difference. Any Ideas?

Populate UITableView with results of webservice

I have a table view which i want to populate with the results (XML) of my call to a web service.
The NSURLConnection and NSMutableURLRequest that are doing the setup for this are currently in my -viewDidLoad method, and i also have all of my UITableView delegate methods in my .m file too.
The data is being returned and added to my array correctly. My problem is (i think) that the UITableView methods are being called before any data has been returned from the web service, which is why my table view is always blank.
How can i call the methods in the right order (if this is even the problem)...
Are you calling reloadData on the tableview once you've repopulated the array? You need to let it know that you have new data.
UITABLEVIEW *mytableview=[[UITABLEVIEW alloc]init];
[mytableview reloadData];
Yes you are right; the UITABLEVIEW delegates are called before the web service. So you have to call those delegates again so try reloading the data. Works perfect for me. reload table when u have something returned from the webservice. store it in NSArray or whatever and then reload table.
Check out Apple's SeismicXML code example on the developer.apple.com/iphone site. It shows how to use NSURL, NSXMLParser, and a TableView to do just this type of thing.

Resources