Editing controls disappear on reload - iphone

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!

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

Update table cell on notification

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.

Calling cellForRowAtIndexPath through an instance

I was wondering if the following scenario is possible.
I have a UITableViewController class which contains ten rows displaying some data. Now, I want to show the first five rows in one tableview and the last five in another. Since I don't want to fetch the data again I was hoping to create an instance of my original table class and call its cellForRowAtIndexPath:(NSIndexPath*)indexPath to return the table cells and add it to the other two table views. Is it possible to call a delegate method through an instance? If so, how can I create an NSIndexPath object pointing to a particular row in any section of the table...
Thanks for the replies. I tried calling the cellForRowAtIndexPath:(NSIndexPath*)indexPath but it always shows the following warning
'RootViewController' may not respond to '-cellForRowAtIndexPath:'
Also the application crashes with the message
***Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[RootViewController cellForRowAtIndexPath:]: unrecognized selector sent to instance 0x4bad30'
Had initialized NSIndexPath as Andrei described.
Guess its just not possible to call the function.
If you don't want to fetch again, why don't you just add your fetch results to an array?
This would have the advantage that populating your tableview would not require "live fetching" - fetching data as you need it may in some cases make scrolling a tableview slow or stuttering may occur.
However, you could also try to add your tableviewcells to one or more arrays to keep references to them while you pupulate your tableview for the first time.
I don't think you need anything that elaborate.
Have all tables using the same datasource. Choose which items to display in which table based on the tag value for that table element. Call reloadData on all tables when anything changes.
My guess is that using the same UITableViewCell in 2 or more tables would not work.
If you are reusing cells then it's very likely that those duplicated cells will eventually be refreshed with another content.
To protect against this, you would need to clone those cells, in which case it's not feasible to work on a UITableViewCell level.
You're better off storing the data (displayed by the cells) in a array (or other storing way) and share that data across multiple tables.
Plus, you'd have the extra ability to customize the appearance of those tables independently while sharing the same content.
To answer your last question, you can create a NSIndexPath to point to any cell.
The first index is the sectio, and the second index is the row in that section.
For example, to point to section 2, cell 5 you can write something like this:
NSIndexPath* myIndexPath = [[NSIndexPath indexPathWithIndex:2] indexPathByAddingIndex:5];
//Or, with another method:
NSIndexPath* myIndexPath = [NSIndexPath indexPathWithIndexes:indexes_array length:count];
Remember that cellForRowAtIndexPath: is not a method of UITableViewController. It is a method of UITableView. With this in mind you would actually call the method like so:
UITableViewCell *cell;
cell = [yourTableView cellForRowAtIndexPath: indexPath];
Doing so should return the cell at indexPath.

Getting a table to redraw

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