Getting a table to redraw - iphone

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];

Related

UITableViewCell is frozen (not rerendering with new data). How to fix?

Having some issues with a tableViewCell freezing. I have a table view cell with a list of exercises. When I add a new exercise on another viewController, then pop back to this tableViewController that lists all the exercises, the respective cell gets frozen (see screenshot). Then, if I call [self.tableCell reloadData], that cell never refreshes.
it's weird because if the offending cell is, say, index #4, and I change the data source to only have 2 items, the 4th cell is still frozen. (the first 2 render correctly, the 3rd is blank, 4th is offending cell)
List of all exercises:
When I change the data in the tableView data source, that cell is still frozen:
If I put a breakpoint and inspect the tableView, it doesn't say anything about that frozen cell:
Not sure where to go from here :( It seems to only happen when I insert a row from a different viewController then pop back to this one.
Any thoughts? Thanks so much for any help!
You are keeping a reference to a cell. That is a bad idea. It contradicts the whole pattern of datasource and recycling of table cells. All kinds of wacky bugs will be the inevitable result.
Instead, you should make sure your datasource methods return the correct number of sections and rows, and the appropriate cells and then reload your table not a cell.
[self.tableView reloadData];
So, for example, when the view is returning from adding a new item, make sure the data array you are using is updated and reload.
I exactly don't understand your way of managing tableview along with data source. If you make any change in data source, same changes should be reflected in tableview as well. (Both should be synchronized). You can use:
Table reloadRowsAtIndexPaths:
Table deleteRowsAtIndexPaths:
Table insertRowsAtIndexPaths:
to make changes to your tableview and its cells. Or you can just call [Table reloadData]
Remember: never try to store references of UITableViewCells as they are dequeued again on screen - your reference will be just a trash. Just alter your cells using above 3 methods and you are good to go. If you want to clear something, just ask for it.

UITableViewController is not updating content is I apply manual sorting

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

Editing controls disappear on reload

Having a bit of trouble.
I've got a UITableView in grouped mode. The table is set to always be in editing mode - which is working just fine. All my rows are delete-able. The rows are indented and all of my UITableViewCellEditControls show up great.
Problem is when a row actually gets deleted I update my data source and then call:
deleteRowsAtIndexPaths:
reloadRowsAtIndexPaths: //needs to get called because of table aesthetics
After the reload occurs (I've tried just a simple reloadData, too) all of my UITableViewCellEditControls (the red circles with the minus signs etc) disappear! The rows are still indented, but they're gone.
I've tried the suggestion on this post:
UITableViewController canceling Edit mode animation when calling [table reloadData] inside (void)setEditing
But no luck.
Any help would be REALLY appreciated!
Without looking at any code, I have some suggestions.
According to Apple's UITableView Class Reference: under ReloadData, it says:
Call this method to reload all the data that is used to construct the table, including cells, section headers and footers, index arrays, and so on. For efficiency, the table view redisplays only those rows that are visible. It adjusts offsets if the table shrinks as a result of the reload. 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
Blockquote
it shouldn't be called when inserting and deleting rows.
From what it seems like, you are allowing them to delete individual cells, therefore, I am assuming the following methods are already implemented: tableView:editingStyleForRowAtIndexPath: and tableView:commitEditingStyle:forRowAtIndexPath: In your commitEditingStyle, you should be just calling the delete method with something like this:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
This should remove the need for any reload data. If you don't want to show any animation, you can just use noanimation enumerator instead of the fade I showed in example.
If however, you still think you need to do reload data after every cell deletion, maybe try it with performSelector:afterDelay:
Hopefully, this tid-bit helps!

How to use reloadData method of tableView iphone

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.

How to remove cells from UITableView directly?

is it possible to remove a cell from UITableView directly, with animation? Without changing datasource and then reloading table data?
Thanks.
In UITableView you have the method:
(void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
There's a similar method to remove a section.
However, you had better have your data model updated just before you call or else scrolling the table may yield odd results!
Not sure what you mean here by "changing the datasource" because removing a row is as simple as deleting an object from the array that's populating the table (the array your datasource is using), then reloading the table.
No animation, no datasource swap.