I have one UITableViewController class. I am using NSMutableArray to fill table cells. I am fetching data from databse & adding it to array. But cell is showing old results. How can I use reladData method & where should I call this method in my UITableViewController?
I quote Apple's documentation when to not use it:
The table view's delegate or data
source calls this method when it wants
the table view to completely reload
its data. It should not be called in
the methods that insert or delete
rows, especially within an animation
block implemented with calls to
beginUpdates and endUpdates.
So call it when you changed your model but not using the tableview methods to alter the table like:
deleteRowsAtIndexPaths:withRowAnimation:
deleteSections:withRowAnimation:
insertRowsAtIndexPaths:withRowAnimation:
insertSections:withRowAnimation:
you can reload your table cell using
//fetch data from database.. and update array
//reload table data asper new data
[self.tableView reloadData];
You can (and should) call reloadData: when you want to force the table to be reloaded or when you know it hasn't been drawn yet.
Often, you will do this in your view controller's viewDidLoad: method or viewDidAppear: (or even viewWillAppear:).
Outside of these methods, if you substantially change the contents of the array of data being used to supply data to the table, you'd want to call reloadData:. For example, say you had a list of products with inventory levels for each. On initial view, you might just show everything in the array, in the table. But say you then have a filter applied to show only the products with zero inventory. The array with the products would be reduced to include only those products, so after reducing the array you'd call reloadData: on the table so that the view was updated.
And by the way, as I discovered recently, when you call reloadData:, the table is reloaded asynchronously (and pretty quickly). However, the methods deleteRows..., deleteSections..., insertRows..., and insertSections... (especially between beginUpdates: and endUpdates: calls) are executed synchronously, and can, for large tables, slow your app down (and freeze the UI) considerably. Your mileage may vary, but that was my experience with these methods. I actually went to a model where I use deleteSections... and then use reloadData: (because I was deleting all the sections anyway).
Whenever you call reloadData method, the table view will be refreshed. As you want to refresh the table after loading data from database, you can call reload data when the fetching is done.
// code to fetch data from database
// when done, reload the table
[myTableView reloadData];
Add { didSet { tableview.reloadData() } in front of your instance class of type array.
Example:
Swift class code:
class Data {
var name = ""
var height = ""
}
ViewController code / Instant DataClass of type empty array:
let dataINDataClass = [Data] {
didSet {
tableview.reloadData()
}
Hope this helps.
Related
using a UITableViewController, where I am displaying some products which are sorted based on make year. Whenever user taps in a row (each row represents a single product) a new view gets displayed to edit the same product.
So after save if the user has changed the make year then it should display in main table view at appropriate place (i.e. sorted again accordingly).
To accomplish this I am calling sort() method in.... here sort method returns NSMutableArray type of object.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Sort the records based on number of days.
sortedProducts = [DTOConverter sort:unsortedProducts];
return [sortedProducts count];
}
But the problem is that in table view I am still having the old values in that particular row until I bounce the entire view then it gets populated with new updated values.
Same thing happens in case of a new product gets added.
Please correct me where I am doing wrong, I am sure that the issue might be related to indexPath, but i do not know how to tackle it. Please help.Thanks.
To update tableview data call
[tableView reloadData];
You have to refresh the table or the individual row with one of the following methods:
reloadData
reloadRowsAtIndexPaths:withRowAnimation
reloadSections:withRowAnimation
reloadSectionIndexTitles
See the documentation on each method: UITableView Class Reference
Note: if you are only updating one row, do not call [tableView reloadData]. It is not necessary to reload all of the data in the table.
You may have to set up a delegate in the new view you are loading from your tableview and then set up your tableviewcontroller to implement the delegate method of that new viewcontroller. Then call reload data from that delegate method Here is an example of a delegate: Delegate example
How can I update a tabelview cell when a particular notification received, without reloading table data. notification tells the id of the cell that I need to update. So I want to iterate all table cells and update only the relevant table cell without reloading it. how can i do this
Use [tableView visibleCells] to get a list of all cells that are displayed. Iterate through this array to see if the cell you need to update is displayed, then do whatever you need to. (this method is if you're reusing cells).
You can just use the built in reloadRowsAtIndexPaths:withRowAnimation: instead of trying to usurp the framework's functions. This will efficiently only reload the cell you ask it two not the entire table or even an entire section.
Reloads the specified rows using a certain animation effect.
Reloading a row causes the table view to ask its data source for a new cell for that row. The table animates that new cell in as it
animates the old row out. Call this method if you want to alert the
user that the value of a cell is changing. If, however, notifying the
user is not important—that is, you just want to change the value that
a cell is displaying—you can get the cell for a particular row and set
its new value.
When this method is called in an animation block defined by the beginUpdates and endUpdates methods, it behaves similarly to
deleteRowsAtIndexPaths:withRowAnimation:. The indexes that UITableView
passes to the method are specified in the state of the table view
prior to any updates. This happens regardless of ordering of the
insertion, deletion, and reloading method calls within the animation
block.
Available in iOS 3.0 and later.
I have a drill down tableview that is using sqlite.
The first one is a tableview where i load some categories using a SQL query.
When i click a row it's using the following query to show the appropriate records for the category:
#"SELECT * FROM table WHERE category IS ('%#')", sharedSingleton.category];
This works.
When i click the back button on tableview 2 and return to the category tableview and then touch another category tableview2 still shows the rows for the category i selected the first time.
I'm new to this stuff but i searched a long time and tried releasing objects or set value's to nil but is doesn't seem to help.
Am i in the right direction, how can i refresh or release tableview2?
Thanks in advance.
Yes you are in the right direction.
When selecting a row table 1 you might be calling a method which loads the second table view and assigning the data to be loaded to the corresponding table view,Which is fetched and filled when delegate/ data source methods of table view is called.
You might have created tableview as a different view controller class, which is initialized and data is reloaded for the first time. But even though you are fetching the second set of data its not getting reloaded automatically even after you are assigning it.
After assigning the data you have fetched into the obect which distributes it to tableview Reload the table data once you assign it to. you could call the one line of code to do this
[tableview reloadData];
Regards,
Jackson Sunny Rodrigues
I have a view which is a big table. This table is populated from my array of data. This is all working well. The table drawing perfect.
I pop up another small overlay view where you can set some filters. The filter does its thing and the array of data that gets changed and the array changes in size so that there is more or less items to draw in the table.
When the filter overlay view disappears there is no change to the view with the big table.
So I am guessing I need to call something to update the table or get it to redraw. Is there some kinda of delegate method for this or whats the best way to get a table to completely refresh itself?
Thanks
-Code
[tableView reloadData];
should be called after you modify the data array, to redisplay the data.
UITableView:
- (void)reloadData
to reload table data use
[self.tableView reloadData]
make a IBOutlet UItable *table1 and assign it to your table
then use
[table1 reloadData];
I have a UITableView that implements NSFetchedResultsControllerDelegate. When I tap a cell, I load another viewcontroller which allows me to edit an entity represented by the tablecells. When I'm done editing, it sends a message to the UITableViewController which does a [self.tableview reloadData];
My problem is that reloadData doesn't seem to resort the data after I've modified it. How do I do this. How do I get it to do so?
Not clear what is that you are trying to do. However if you want cells to be in a different order for a table view you must sort its data source, so if first you want it in order 1 2 3, then in your data source they will be in that order, but next if you want them 2 1 3 then you must resort your data source to contain 2 1 3 in that order, it all depends on what you are returning in cellForRowAtIndexPath for each index path, the sample i gave is assuming y ou are using the row indexpath to index into an array that i s your data source..hope this helps
You can pass the NSSortDescriptor to the NSFetchedResultsController, but have you actually executed a fetch?
You need to save the changes using [NSManagedObjectContext -save:] and then your NSFetchedResultsController will pick up the change and notify the table view to update. You do not need to call -reloadData because it will be handled for you.
Make sure that your UITableViewController is the delegate for the NSFetchedResultsController so that it can handle the update.