UITableView's reloadData isn't resorting the cells - iphone

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.

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

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.

How can I return the count of objects for an FRC fetch?

I am trying to use the count of objects from my FRC fetch so that I can create an "Add" cell after all of my fetched objects have been listed in a UITableView. Is there a method for returning the number of objects fetched? I can only find a method that returns the count of sections for a given FRC.
Simplistically, you You want a count of fetchedObjects. So:
[[myFRC fetchedObjects] count];
The problem that you're going to have with adding an additional cell, is that every time the table ask for the count of sections and rows, you're going to have add 1 to the count so the table knows to add the extra row.
Bjarne Mogstad is correct. It's quicker and cleaner to put the add element in a header or footer for the table itself. That way, the table directly and cleanly represents the data structure without having to constantly adjust for the phantom row.
The easiest way to do this is by setting the tableFooterView property of your table view. The footer view will be displayed below the last table view cell.

NSFetchedResultsController and my TableView structure

I have a tableView controller with two sections. The first section has a couple of input fields and is not really displaying core data. The second section displays items from a database saved with Core Data.
I have an NSFetchedResultsController and I serve up data for cellForRowAtIndexPath and didSelectRowAtIndexPath as follows. For section = 0, I manually serve up the appropriate input fields, and for section = 1 I want to use [fetchedResultsController objectAtIndexPath:indexPath]. () However, since the fetched results controller only knows about one section, this doesn't work.
I know I can create a new IndexPath with section = 0, and then feed that to the NSFetchedResultsController. Is that the preferred solution or is there another way to 'tell' the NSFetchedResultsController what to expect?
The only way to do this is to translate the NSIndexPath objects passed into the various UITableViewDataSource/UITableViewDelegate methods into index paths appropriate for your NSFetchedResultsController. I'd recommend adding a method to your class that does this.
The return values from this method will match the section numbers the NSFetchedResultsController uses. Also, if in the future you end up needing a second header section for whatever reason, it's easy enough to adjust your method to do that.
Why would you need to tell it anything? In the delegate methods, just offset the section index by one and you should be fine.
You can create your own NSIndexPath instance to pass into the NSFetchedResultsController to resolve this issue.
Update
If you want to have two sections then yes that is the right answer. However I would consider putting your input fields into the table header instead of a section. That would be a cleaner answer in my opinion.