Reload collection view with new data - iphone

How would I reload new data, say an array of names (strings), without re initializing the collection view with data?
I know of [uiCollectionView reloadData], but I wanted to changed the array of information associated with the collection view.

reloadData causes the collection view to ask its dataSource about what it now contains. Whatever the dataSource uses to decide, this is your collection view source data. If you change that data then you need to call reloadData for your UI to get updated. You can change the data however you want (like adding / removing items from the array or creating an entirely new array).
You never initialise the collection view with data. You initialise it with a delegate and dataSource. So you aren't re-initialising it. You're just changing the data that the dataSource is providing to the collection view.

Related

How to write custom accessors for an NSManagedObject subclass?

I have a view called Cart. It displays a table of Items. These Items need to persist, so Item subclasses NSManagedObject; values like id, price, etc. are properties whose accessors are #dynamic so that they are automagically generated.
I have another view called Favorites. It displays a table of Items, but they don't need to persist. In fact, this view changes whenever the user logs in with different credentials.
The connection between the two views is that the user can add items to his cart from his favorites. A cart can store Items from different Favorites lists. Favorites lists don't change when the item is added to the cart.
Initially, I made the model for the Favorites view be an NSArray of NSDictionary objects. When the user adds the item to his cart, I create and save the item in Core Data from the NSDictionary key-value pairs. This approach doesn't look very clean or very DRY. Wouldn't it make more sense to make the Favorites view's model be an NSArray of Items?
So now my intent is to implement the Item class so that it will represent the Core Data model (NSManagedObject), but also work with the Favorites view. Being new to Objective-C and iOS development, I really don't know how this would work or look like. It seems that I would need to override the accessors that are magically created for me, but I can't call them at compile time using a super call... Can anyone give me a rough outline of when it would know to return the NSDictionary data or the Core Data data? In the case that it's Core Data data, how do I maintain the same level of efficiency that the magically-generated accessors would have?
Even better, is there a better implementation that is just as DRY or makes more sense? Or am I trying to combine too much functionality into one class? Is the NSArray of NSDictionary objects the best way to go in this case?
You can specify a result type on your fetch request (object, objectID, count, dictionary).
Also, I would not use NSManagedObjects outside of a MOC. You should either have a separate object that you use for memory stuff, or you can use an in-memory persistent store for those objects... or, you can just create a separate MOC as a child to your main database MOC that you use for your in-memory objects.
The advantage of these approaches is that your code does not have to know whether they are backed to the disk or not.
As long as you do not save the MOC, the changes to those objects will never go to disk.
EDIT
NSFetchRequest *fetchRequest = // create the fetch request...
fetchRequest.resultType = NSDictionaryResultType;
Now, when you make the fetch, instead of getting back an array of NSManagedObject you will get back an array of NSDictionary.

Edit record un tableview using Core Data

I am new to core data .I have already performed insert and delete operations but a am not able to get how to perform Update (edit) in it. I am using a Tableview and on the detailview of it i want to edit my data which will reflect core data.
How can i do this ?
pass the selected NSManagedObject (or your subclass of it) to the DetailViewController and edit its properties.
If you have used NSFetchedResultsController and its delegate methods the main tableView should update when you return from the DetailViewController. If you haven't used them reload the table manually upon return (probably in MainViewControllers viewWillAppear: method).
Check out the examples given in Chapter 7 and 8 in below link:
http://www.headfirstlabs.com/books/hfiphonedev/
Then you only have to handle array in which you fetch records from database and then again write data into database.

Can I remove an object from the fetchedResultsController without deleting the object from the db?

I have been trying to remove an object from the tableView I am working with without deleting the object from the db, and I can't figure out how to do it.
I have tried setting a predicate on the fetchedResultsController, to filter objects that have a BOOL set to a certain value. Then, when I change that value, I expect that I can get that object to stay in the db, but out of the fetchedResultsController because of the predicate, but alas, that isn't working.
How can I remove an object from my tableView's dataSource (the fetchedResultsController) without deleting it from the core data db completely?
Please help! I've been bashing my head against this for way too long
I've always rolled my own table view data source when I need to weed out fetched results. NSFetchedResultsController objects are great when you want to show everything in your data base, but not so great when you want to weed out some of the data on the fly. I fetch the data, then iterate through the results array looking for the data I want to keep. The good objects get added to a new array, which becomes the basis of my table view data source.
The times I've implemented this, the data manipulation happens in a model object which hands off the array to the UITableViewController subclass that implements UITabelViewDataSource protocol methods. I suppose you could implement this as a subclass of NSFetchedResultsController, but I've never tried that approach.

Should I fetch all objects initially or when each view controller is loaded?

thanks right away. This is my first question and am excited to join the iOS developer community. I have one core data entity (say, a car). I have a tab view controller with two tabs - one displaying all cars and another displaying all types of cars (Chevy, Ford, etc.). The question deals with this second view controller. My question is - do I want to fetch all of my cars when the tab is loaded then pass all relevant cars of that type when the row is selected, or do I want to fetch my results after a row is selected meaning I'd have a different view controller for each type of car?
UPDATE:
I do, indeed, have two table views. The second one with the types has a list of types. When a row is selected I'm curious if I should pass the relevant cars to this VC or fetch the results?
I'd say you use 2 ViewControllers: 1 for all the Cars and then 1 for all the relevant cars of that type. You can just pass the relevant data (let's say an array of carmodels) from the FirstVC (where you put your initial array of cars , or dictionary if you will) to the secondVC and adjust your VC-looks accordingly.
(Seems you want to work with a UITableView to do this, it is very simple to pass these kind of messages through to a new VC and do the customization you want anyway)
You will all find your UITableView answers right here in the AppleDoc: http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UITableView_Class/Reference/Reference.html
A UITableView object must have an object that acts as a data source and an object that acts as a delegate; typically these objects are either the application delegate or, more frequently, a custom UITableViewController object. The data source must adopt the UITableViewDataSource protocol and the delegate must adopt the UITableViewDelegate protocol. The data source provides information that UITableView needs to construct tables and manages the data model when rows of a table are inserted, deleted, or reordered. The delegate provides the cells used by tables and performs other tasks, such as managing accessory views and selections.
One of these delegate methods you seek is the
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
This method will trigger when you select a row in your tableView. In this method you want to call and show your new VC and this is also when you pass the relevant data to that new VC. The rest of the above will still hold up then.
Good luck.

Maintaining data sync between UITableView cells and array of model objects

I have a UITableView where each cell corresponds to a model object.The list of these model objects are kept in an array inside a singleton object that manages the model objects. The UITableViewController subclass holds a instance variable that references this singleton object. The model objects update their internal data asynchronously from the web. What's the best method for updating the table cells when the corresponding model object finishes reloading its data? Should the model objects send out a notification? Can the table cells use KVO to receive changes from the model objects? Is there another option? What is the best practice here?
I found a solution by subclassing UITableViewCell. Each cell maintains a reference to the model object that it corresponds to and observes a boolean isLoading property of this object. When the loading state changes, the cell updates it's data. In other words, the cell (view) object observes the model object, then requests data to present upon the model object state changing.
I'm not sure that is the best solution but in a previous project I was calling the message reloadRowsAtIndexPaths:
NSArray* paths = [[NSArray alloc] initWithObjects:indexPath, nil];
[tableView reloadRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationMiddle];