json webservice and uitableview - ios

I have JSON web-service and my task is to parse the web service and store the data into UITableview.
I am parsing the web service using NSURLConnection methods.
After parsing the web-service i got the response what I wanted .
But the issue is that my UIViewController is calling UITableView Delegate and Datasource methods first and then it calls web-service so my UITableView doesn't get any response.
Even though I call the web-service first it calls all the UITableView Delegate and Datasource methods first and then web-service so my table view doesn't get any response.
After calling the web-service i get perfect response.
So if somebody knows any solution please help me.
Thank you.

The other answers in this thread should solve your problem.
However, if you find that [self.tableView reloadData] isn't refreshing the data in your UITableView, check out my suggestion on this thread:
Fails to call delegate/datasource methods in UITableView implementation
I'm not sure if this is a quirk with iOS 8, but I've had a few occassions where just setting the table's dataSource and delegate in the code hasn't been enough.

After you get your response and populate dataSource just call
[self.tableView reloadData];

Just set the data-source and delegate of table view in the delegate of the web-service delegate or where you get the response and reload the table view.

Related

UITableView weird reloadData issue

I am stuck in a really weird issue, What em doing is calling NSURLConnection sendAsynchronousRequest in which i reload tableView and it does it without any issue. This scenario works fine while em standing on the same ViewController while request response is received. The issue arises when I call sendAsynchronousRequest and move from that ViewController and then come back to it before the request gives the response. This time the TableView reloadData gets called but doesn't do anything on the View. I call the reloadData in this dispatch_async(dispatch_get_main_queue(), ^{}); for it to run on mainThread.
Any help in this regard would be great.
Edit 1:
Here's what i have gotten far till now.. when i start the NSURLConnection asyncRequest it keeps the reference to the tableView and array that it has at that current time.. But when i navigate off from that view and then come back the view is reinitialized so the tableView and array has new references. So when the asyncRequest calls tableView ReloadData, it calls reload for the older referenced tableView rather than the one that is now visible hence not reloading the tableView… hope now you can help.
Rather than reloading tableview on main thread, try to reload it once you get data and the data is parsed. Switching between views may cause your tableview to reload before the data comes from server resulting blank tableview and this is caused due to calling some methods on main thread.
EDIT: Try to initialize tableview in viewdidload only and array just after you get response from server and before loading data in it.

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.

ASIHTTPRequest async updating uitableview

I'm using a UITableView which loads data from a server. I need to use async ASIHTTPRequest, which is in a separate .m.
How could I force to reload the table data when the requestFinished:request is called?
Thanks!
You can just send the data that needs to be loaded into the TableView by passing it between files. and then finally call a different method of inside the table view that implements the command [self.tableview relaodData];
Update:
What i see from your comments is that the you have a tableView CONtroller or someform of controller that loads the data by calling another class that holds the ASIHTTPrequest/ ansynchrinous requsts. now You have also told me that the TableView Also hold a UIViewController in each cell which i Dont think is a good idea you could go with a custom cell method have a look at this 4 part tutorial... now and the data that needs to be loaded into the TableView is loaded by TableViewController by some method wither it be each cell or something... so after you are able to get the data just inside the tableViewController just Call the [self.tablebview reloadData] inside the TableViewController which is the parent of the TableView there is no need to call in the other methods.
Finally solved with observers although when the app is completely closed won't work. I'll think in something.
Thanks

help core data tableview not reloading

i am using core data...my table view does not reload say if i add a new row to the table. until i terminate the app and re open it...im calling reloadData in my root controller viewwillappear method... any help would be appreciated
thanks in advance
I am guessing you are using a NSFetchedResultsController? If you are not then you need to be with a UITableView. Once you do, make sure you have your UITableViewDatasource configured as the delegate to the NSFetchedResultsController. Once you do that, you will not need to call -reloadData at all because the NSFetchedResultsController will monitor changes in the NSManagedObjectContext and update the UITableViewDatasource as needed via the delegate methods.

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