Reload data in UItableview using a button - iphone

I want to add a refresh button in UINavigationBar that refreshes the entire table contents. Nothing happens when i click on the refresh button.
I searched google and found that reloadData method refreshes the tableview so I tried using it.

Are you sure your tableView outlet is linked correctly to your tableView property in Interface Builder ?
Is your internal datasource `refreshed' before you call reloadData?

-[UITableView reloadData] tells the table view to request again from you the number of rows in the table, and the cells at each section and row. It's up to you to update your internal data structures as appropriate. So in this case, your IBAction should call your own method to refetch the data from the server.
A few other notes:
Method names should have leading lowercase.
-initWithContentsOfURL: is blocking. This can hang your program for a long time. You should be using asynchronous fetching here (generally with NSURLConnection).

make sure you have the tableView outlet hooked up to the tableView.

Related

Detect when NSTabview reloads in Swift

I am trying to add a label to the window whenever the tableview item count is zero. I figured that the best way to do this would be to check tableView.numberOfRows after tableview reloads . However, I can't seem to find a way to detect it when the tableview reloads. Is there a way to do this?
Table views don't spontaneously reload. You tell them when to reload. You should probably have a central data model object, and have it notify your view controller when the items count reaches zero. (You could set up your data model object to have a delegate, for example, and give the delegate a itemCountChanged(to:) method.)

TableView is not showing data?

I have place this question before on following link with code & screenshot.
TableView is not loading data?
Now I came to know my tableview is loading data in tableview cell but not showing/displaying it. I have one Model View Controller in my project. The ModelView controller get called from Mapviewcontroller screen through Search Button. After resigning the Model view controller I can see the data in tableview. (It means when I move to next controller and back to previous tableview.. my tableview shows data in it.)
Here I my data get visible in tableview only after resigning the modelview controller.....
Everything works fine. I just want to know how should I display the tableview data as soon as web-service get called on first screen itself???
Put [_tableView reloadData]; at the end of the method that calls the web-service, or the one that populates the markers array.
If you are manipulating table data in an asynchronous Block,
pass a reference of the tableview to the method implementing the block, and remember to call
[tableview reloadData];
within the block, and not out outside

UITableView cellForRowAtIndexPath occasionally not called?

I'm developing a graphing application that on the main navigation/tab view displays a UIView that renders a graph using openGL. Beneath that view is a UITableView that displays the list of elements on the graph, and an add button.
Occasionally, when the user clicks on another tab, and then returns to the graph view tab, the table view does not get redrawn.
I have a [tableView reloadData] method being called in the navigation controllers' (also the table view's delegate and data source) viewDidAppear method.
numberOfSectionsInTableView and numberOfRowsInSection get called, but cellForRowAtIndexPath does not despite both latter methods returning positive values.
This is an intermittant problem, only happening some of the time, but it's not clear what (if anything) influences this.
Does anyone have any ideas?
[edit] Should just quickly add; clicking or otherwise interacting with the table view, whether faulted as described or not, ALWAYS causes it to be sucessfully redrawn.
[And again] A bit more information; when it faults as described, and calls the first two but not the cellforRowetc method, it seems to wait until the user interacts with the table before cellForRowetc is finally called (the first two methods are then not called) and is redrawn.
I had a UIActivityIndicator, started on a seperate thread, that showed when requests were being made to a web service. When the web service calls were completed, a notification was sent to the graph controller class to stop and hide the UIActivityIndicator. In this method, reloadData was being called on the table whether the view was visible or not, which seemed to be causing the problem.
The conclusion seems to be not to call reloadData on a table unless it's visible.

What's the best way to refresh a UITableView within a UINavigationController hierarchy

I'm pretty new to iPhone development and have struggled to find what I consider to be a neat way around this problem.
I have a user interface where a summary of record data is displayed in a table inside a navigation controller. When the user clicks the accessory button for a row, a new view is pushed onto the navigation controller revealing a view where the user can edit the data in the corresponding record. Once done, the editing view is popped from the navigation controller's stack and the user is returned to the table view.
My problem is that when the user returns to the table view, the table still shows the state of the data before the record was edited. I must therefore reload the table data to show the changes.
It doesn't seem possible to reload the table data before it is displayed as the call only updates displayed records. Reloading it after the table has been displayed results in the old data changing before the user's eyes, which I'm not too happy with.
This seems to me like a pretty normal thing to want to do in an iPhone app.
Can anyone please suggest the best practice approach to doing this? I feel like I'm missing something.
Cheers - Steve.
The standard approach may sound like a lot of hassle at first, but is a useful pattern for a lot of situations.
In your tableview class create a method like:
-(void)editDone {
[self.tableView reloadData];
}
Add a property to your edit controller like:
#property (assign) id delegate;
Set the delegate when your accessory is clicked:
editController.delegate = self;
And when editing is complete, call your method like so:
[delegate performSelector:#selector(editDone) withObject:nil];
You can create similar methods to handle cancel of your edit component, or to carry out dismissing of modal edit controllers, etc. It's considered more classy to put all this in a protocol, if you like.
I'd implement this in the following way:
Save indexPath of a clicked cell.
Implement -[UIViewController viewWillAppear:] method of the view controller, which contains the UITableView. If saved indexPath is not nil, reload specified cells with:
-[UITableView reloadRowsAtIndexPaths:withRowAnimation:]

Why UITableView methods are being called before viewWillAppear?

I was looking forward to find a cause for my application being crushed at some point and I found out that methods of UITableView are being called before or at the same time as viewWillAppear is called.
In viewWillAppear I have code that is initializing number of rows in each section but numberOfRowsInSection is being called before I finished setting up array that has amount of rows in each section.
I believe that viewDidLoad is not suitable in my case because it is being called only once after launching an application. Am I right? And I need to make my initialization function called each time a view appears on the screen.
How can I overcome this failure?
Thank you in advance.
Well, I think this 'problem' has to do with that there are multiple threads running taking care of the view and the UITableView.
The view calling this view could (before switching to this view) call a method on the View which gathers the data.
User pushed button
You fire a method on the destination view, gathering information
Switch to view
You could work with delegates to know when the destination view is ready loading the data you needed, so you can switch to that view then.
Hope this helps.
I think the right answer is that "[super viewWillAppear:animated]" will call UITableView's method.So it is necessary to put this code after the code that initialize the tableView.