Saving Array content over two view controllers - uitableview

i have a slightly confusing problem here. I am using storyboard i have a tab view controller tab 1 has a table view controller (table 1) which on selection of a row goes to table 2 and some data is added to the table 2 here n it is shown on table 2 cell when i navigate back to table 1 and go forward again to table 2 that data is lost ..how can i save the data then so that it can still show the saved contents. thanks

It's because the navigation controller releases the second view controller when you go back from table 2 to table 1. If there were no other references to the 2nd table view, it gets deallocated. I suppose you're also creating the table view every time you select a row in table 1. Try to keep a reference on the second table view, and not re-create it every time, only on initialization.

Sounds like you need fixed data storage. You can use Apple's Core data for this. See Apples documentation on Core Data or see "Pro Core Data for iOS" by Michael Privat. Then each view controller can store and retrieve data from the permanent store. Be aware that learning core data takes some effort, but it is worth because you will find yourself using it time and again. I also ran across this project arraydatatable which provides a structured data solution without using core data. Also, since you mentioned that your are new to iOS you should read Apple's documentation on their recommended Model View Controller (MVC) structure. In that structure the Model is your data, the View is what you see on the screen, and the Controller controls the view as well as fetching and storing data to the model.

Related

Consolidate Table data into one

I have 4 different views, all that have their own table view with data. I want to make a 5th view/interface that has a table view that consolidates the other 4 tableview data into one.
For each of the 4 initial table views, I am storing the data using Core Data, and each table has its own entity with multiple attributes. Im looking for the theory about how to consolidate all these into one tableview. Any help would be great, or if you can point me in the right direction.
So essentially i have 4 table views all with their own data, and i want to make a 5th table view that shows all the data.
Thanks so much!
I guess what I would do is to store all the data of all the table views in a single mutable dictionary that contains sub dictionaries that contains each table view data like so:
MainDictionary >
---tableview1Dictionary
---tableview2Dictionary
---tableview3Dictionary
---tableview4Dictionary
I would save this dictionary on the NSUserDefaults so it can be accessed from each view controller in your app.
On the fifth view controller you can simply load this MainDictionary data in to your "combined" table view. This way whatever changes you make on any table view will directly effect your combined table view and all your data will be synced.
You can reload each table view on your controller viewDidAppear.
NSUserDefaults Tutorial: http://www.icodeblog.com/2008/10/03/iphone-programming-tutorial-savingretrieving-data-using-nsuserdefaults/
Hope that helps.

Moving cells from one table to another in iOS

I am trying to create an application with multiple table views that uses and implements Core Data. I would like the user to be able to select cells in one table and move them to another (like in Apple's mail application) using either a check accessory or a selectedCell method with an action sheet. I'm stuck because I don't know if you are actually moving the cell to another table or if you are adding a copy to the new table and deleting the original. Basically, I'm asking for a basic example of cell movement to give me a push in the right direction.
You won't be moving cells. The model for a table view is an array. Move things between the arrays and tell the tables that their model has changed.
id somePartOfMyModel = [self.arrayA objectAtIndex:someIndexPath.row];
[self.arrayA removeObject:somePartOfMyModel];
[self.arrayB addObject:somePartOfMyModel];
// the simplest, non-animated way to update the tables.
// I'd advise getting this working first, then later trying fancier UI to indicate changes
[self.tableViewA reloadData];
[self.tableViewB reloadData];
You would not be technically moving the cell to the other table. The way I would go about doing this would be to pass the NSManagedObjectContextID of the item between the tables, depending on how large your entities are and if the tables are in the same view controller.

UITableViewController data source from coredata

I have found two ways of populating UITableView with data from coredata. One way is to use NSFetchedResultsController and implement the delegates for update controller:didChangeSection. Other way is to copy data from coredata to local array of Managed objects. Handle the updates on local array and saves the changes to managedcontex.
Both could be found on developer.apple.com.
I would like to hear pros and cons for each metod?
Thanks.
It is better to use NSFetchedResults controller, because your current view controller may not be the only one changing the data. It may happen in the background, or through some other view controller (for example, if you have a split view controller, you may be changing data related to a record in the master view controller, in the detail view controller). In those cases, you want your table to automatically reflect the changes. In extremely simple cases, where you are the only view controller in town, it may be easier the other way, but do it the right way and you will be happy.

Load up entity from db rather than pulling data directly from db

I created an iOS app that allowed me to enter data into database and then display in tableview (using NSFetchedResultsController & tableView cellForRowAtIndexPath). This worked. Now, I added 3 one-to-many relationships and another entity and I need to load up the entity (on the one side of the one-to-many) from the db instead of pulling it directly from the db so that the data can be used in the relationships. Do I still use NSFetchedResultsController?
relationshipEntity2 is the one-to-many relationship between Entity1 & Entity2. The many point to Entity2
I know I'm supposed to use:
Entity2 *entity2 = [[self.entity1.relationshipEntity2 allObjects]
objectAtIndex:indexPath.row];
I am new to iOS development and even newer to Core Data but I must learn it. Any bit of help or pointing to a book or tutorial I haven't come across yet would be greatly appreciated. I haven't had much luck finding anything that does what I'm supposed to be doing.
Thanks and have a great week!
-------added for more description on project-------
I'm given the task of having a View Controller with a 3-part segmented button. There is also an add UIButton that pops up a view (bringing subview to the front, not a popover segue) with a UITextField for input to add to the table view on the view controller. There are 2 entities and three 1-to-many relationships. There is 1 relationship for each button on the segmented button. If the user, has the first part of the segmented button selected, adding a value to the popup textbox, should only add it to the table view seen when the first segmented button is selected. I have the CoreDataGeneratedAccessors created. I had this project saving data to database and fetching data to present in the table view but that was before the segmented part was added. Now I have to figure out out to separate the data into "collections" and then show all of say the first collection when the first segment of the segmented button is chosen. I'm just learning and just figured it out without the segmented part and now it's changed on me. I've worked with relational databases but in iOS it seems like it's new again.
Your question is by no means clear. You are not describing any problem, nor do you explain intelligibly what you want to accomplish.
Trying to infer your meaning: yes, you would continue to use your fetchedResultsController. This controller gives you the right object for each indexPath. When building your cell contents, you can easily get to the relationship entities with entity.relationship.
The code above will not work because entity.relationship returns an NSSet which is an unordered group of objects. (The additional allObjects does nothing and is redundant.) Therefore, objectAtIndex will not work (this only works for NSArray objects).
Hope this helps.

Data transfer between two view controller

i m pretty stuck on this one rite now let me explain the problem this way:
Motor View Controller (table 1)
Add New Motor (table 2)
show motor Detail (View 1)
edit Motor Detail (table 3)
now what happens is i go to table 1 then click on add button then go to table 2 and add a new motor on table 1 when i click on row of table 1 i go to view 1 where my details are shown in the labels and wen i click the edit button on this view a edit screen pops up now when i edit here i wanna save these details back onto my view 1 how do i do it ?( im using storyboard and all views are connected through segue's ) (basically i want to do edit function so wen the the edit screen comes up i want to see the existing details in my text boxes n den i get to edit dem n den save them back)thank you
I have two answers for you.
You need a permanent store for your data. Check out Apple's Core Data. They have documentation and an example. Also see RayWenderLick's Tutorial on Core Data. The answer is you will use Core Data to create a persistent store creating an sql database. The database will have an entity for motors. Each entry for the database will contain the detail for one motor. Each view controller (table) will access the database.
This solution provides you with a persistent store. If your power the phone off and on again you will still have your data.
A quick solution is to setup a delegate protocol which would allow the edit view controller to write data back to one of the other view controllers. Read Understanding Fundamental Design Patterns.
This solution may get you past your immediate problems but does not provide a persistent store.

Resources