Refresh tableviewcontroller in ios - ios

I am trying to refresh the table view controller when certain task is performed inside a block.I have checked questions on stackoverflow but they deal with refresh option but i want it to just reload view and go to viewDidLoad and ViewWillAppear.This was the suggestions.
[self.tableView reloadData];
but it doesn't seem to work.Any ideas?

You can not reload tableview or update any view inside a block. If you want to update or refresh your table then you will have create a main threaded block and write your code for table refresh on the main thread.
So, try this code:
dispatch_async(dispatch_get_main_queue(), ^{
enter code here
});

Related

How do display a refreshed view without touching it first?

Well, the title already says it all. I have a view (a table view in my special case embedded in a non-table view controller) and at some time in code I want to update cells and display the update. The first part works like a charm but the changes won't displayed after the reloadData() - and even when I call setNeedsDisplay() afterwards.
When the controller has to update something the tableview will be also re-drawn and the changes are visible. As soon as I touch the tableview cell the changes are also visible. Without touch the update just isn't displayed.
I've come across this issue and using GCD should solve it
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});

Why do table cells appear with delay?

I have a tableviewcontroller. In viewdidload, I call a method which gets some information in josn format from a backend. After receiving my desired response and serialisation, I reload the tableview.
My problem is that the content of the cells will appear within about 30 second delay than table view.
Does any one know how can I fix it?
Have you tried reloading tableView on main thread?
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});

UICollectionView Reloading Data

A question regarding reloading data in your UICollectionView.
When using reloadData it only does this once any scrolling has finished. Then there is a small pause where the collection view freezes and cannot be interacted with until the new data is loaded in. How can I get around this?
For example, I would have a view comprised of maybe 10/20/30 items, each with an image and label. I then update my dictionary with new images and strings and then reload the view to add these new items.
Many thanks.
EDIT:
On closer inspection, its not the reloading that causes the pause, its some other code, where I fetch some data. Why might this freeze my view though when i am not even reloading it to make changes to it?
I am fetching this data from -(void)scrollViewDidScroll:(UIScrollView *)scrollView
EDIT 2:
Ok here is some basic code
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self myMethodWithCompletionBlock:^(NSMutableDictionary *results, NSString *nextPageToken) {
//No code in completion block is ever called when the method is called in the dispatch block! Regardless of what it is.
}
});

how to create refresh button in UITableView

I want to create one application that read many data from json and show them in tableview.
I want to create one button that when to click on it this page for a second time to be refresh and take data from json (check json again and if this json changed tableview also changed)
I dont know how create this function that refresh this page and run code at first.
please tell me about it.
[self.tableView reloadData]
to load more data from webserver in to table
Put a button in header of section. I think that is the best place. In target method of that button call json and download all data. then call [self.tableView reloadData]. It will call all table methods like number of row , sections, cellForRow.
Why don't you use the Default RefreshControl provided in iPhoneSDK.
A UIRefreshControl object provides a standard control that can be used to initiate the refreshing of a table view’s contents. You link a refresh control to a table through an associated table view controller object. The table view controller handles the work of adding the control to the table’s visual appearance and managing the display of that control in response to appropriate user gestures.
Check this to learn about its implementation : Working with UIRefreshControl
You can place a UIToolBar above the tableview. In that you can have a barbutton, to which you could give an action to fetch the data again. Once the data fetch is done, call
[self.tableview reloadData];

Reload TableView Tab Bar Application

I have a tab bar application that displays tableviews in two of the tabs. The tableviews are populated using NSFileManager to read the contents of a plist file stored with user data. One of the tabs displays the "complete" items, the other tab displays the "Incomplete" items. As the user selects a row, a page with the details of the item selected is displayed. In the incomplete table there is a button the user can press to move these details from the "Incomplete" table to a "Completed" table. The plist is then updated using NSFileManager to change a field detailing if the item is in the complete or the incomplete list for this item.
The problem I have is that the changes are not updated on the two tables just by selecting between the two tabs. The user has to quit the application and start it up again to see the item moved from one table to the other.
The data seems to be updated its just the view that doesn't update.
I'm new to cocoa so any help at all would be greatly appreciated.
As you haven't provided the code, so i cannot tell where exactly you should reload your tableview.
You have to call
[self.tableView reloadData];
just after you are finished using NSFileManager to save your data in the plist.
Table views are not updating because you are probably not calling [self.tableview reloadData]; when you call reloadData for a tableview it should update the tableviews.
Since you are using tab bar controllers when each view is pushed to the stack it stays there you need to detect if view is appear again and call reloadData method.
in completed.m add this:
-(void) viewWillAppear:(BOOL)animated{
[self.tableview reloadData];
}
in incomplete.m detect if NSFileManager is saved data then call [self.tableview reloadData];
there are also other ways to solve this problem, you can communicate between viewcontrollers via Delegate or NSNotifications pattern. So lets say you saved the data you call your delegate or NSNotification method and receiver view controller runs your desired method. In your case refreshes the tableview
How do I set up a simple delegate to communicate between two view controllers?
http://mobile.tutsplus.com/tutorials/iphone/ios-sdk_nsnotificationcenter/

Resources