add row uitableview-coreData - iphone

I have a table view, and I want to add a row, and save it to coredata.
So is this a good practice:
when user clicks the "plus button" on navigation bar, on the screen will appear new view controller, where user can write required data for new row. When the user clicks "done" button, controller by delegation will trigger an addRow method (from RootViewController) which save data to database.

Yes, it does seem fine. The delegation is also responsible for the table view reloading once you added new entry, if your data source is stored in NSArray or other collection.
If you use NSFetchedResultsController, it will reload automatically once detects the changes made to Core Data entity.

Related

Moving table view cells from one view controller to another

I am new to stack overflow and a student currently learning objective-C at university. I am building an APP for the science museum in London and I'm creating an events planner.
I have two table views set up in two different View Controllers.
The first View controller and table view is called "Events" and it holds all of the current days events. When you click on an event, it goes into a new View Controller, gives more information about the event and has a button to "Add To Events", which pops up an alert saying: "Are you sure you want to add this to your events?" with an add button and dismiss button accordingly.
The information in this table view is populated using three NSMutableArray's. (One for title, subtitle and image).
The second view controller has an empty table view inside it. I am trying to make it so whenever a user finds an event they like, they can click into it, see more info and if they want to add it to their own events page, they can. I have got the "Add" button of the alert responding using an NSLog message, so the code to implement the adding to events would go there.
My question is, if i click on the first event, and then choose to add it to my events, how do i send the information of that specific tableviewcell that i clicked to display in the second view controllers table view ?
I have looked all over the place for information regarding this and have taken an abundance of Lynda courses online about IOS and objective-C, but I haven't been able to figure it all out.
Can anybody help?
First of all you shouldn't use three NSMutableArray's to populate your cells. Create one NSMutableArray and populate it with NSDictionarys with a key for the title, the subtitle and the image. Or even better: create a custom model (subclass of NSObject) for your Events and populate the NSMutableArray with those.
Now just like your NSMutableArray is the data source for your first table view controller you need another NSMutableArray as the data source for the second table view controller. When a user now clicks on "Add To Events" all you have to do is add the Event (Model or Dictionary) to the NSMutableArray of the second table view controller and either call - (void)reloadData on your table view so that it reloads ALL data or use the "Inserting, Deleting, and Moving Rows and Sections" methods from the UITableView Class Reference. This would be the better approach because it does not reload data that does not need to be reloaded.

Save the data entered in textfields,date picker,etc in a view(page) to a Uitableview

i have a view(page) with lots of textfields,date pickers, etc. This page is displayed using presentModalViewController. Now i want to save the data entered in the textfields to the tableView. Each textfields in the page corresponds to the column in the tableview.
I want to save the page's data on click of a button to the tableview as row wise.
Any Help !!
You could use a protocol and delegate object. Here is a simple example that will show you how to accomplish that:
http://www.theappcodeblog.com/2011/04/15/passing-data-between-views-tutorial-using-a-protocol-delegate-in-your-iphone-app/
Your View is presented from a ViewController, which is Controlling the View.
On clicking the save button on the View, the ViewController can pull the data from the fields in the View, and save them into a Model object which you can then add into your UITableViewDataSource array (or whatever you have to load data into your table).
You can then tell your UITableView to update row at index path, and it will pull the new data from the datasource and update your table.
Voila....
... a cleverer (is that a word) approach would be to bind your Model properties to your View elements (from your controller), so as they are updated, the model changes. The table could be using KVO to listen for Model changes and update their view components at the same time.

how to share data between views with navigation controller iphone

i have a view which is fetching data from an Array and presenting the data in a tableview. This view has a navigation controller with a button in it. The button is meant to take you to another view for advanced searching. Let's say that in this new view i have a picker, when the user selects a value from the picker and clicks the back button in the navigation bar i want to get the value that the user selected. What is the best practice to do that? How can i send the selected value from one activity to the previous one?
Thanks in advance.
What you need here is reasoning a bit in terms of the Model-View-Controller design pattern.
Views should get their data through the model. So in your advanced search view, when the user selects some value, this value is stored in the model.
When you go back, the first view redraw itself by reading the current search value from the model.
There are other possibilities, like having the search view controller own a pointer to the first view and sending a message to it when the search value changes, but this is not very modular and is pretty fragile.
Use delegation. Write your own protocol like "PickerViewDelegate". then implement this protocol in your "main view" (which has table view). in PickerView just invoke [delegate somethingPicked:something].
I'm not sure, that search value is a model entity.

Table not updating when datasource is updated

I have a tab-Bar Controller + UINavigation Controller app.
In tabs 1,2,3 I have table views that offer the user some items to choose from. Once the user selects the items they get added to a Cart.
Tab 4 is a table that shows the items in Cart.
I noticed that when the user selects items in tabs 1 or 2 or 3 they show ip in the cart tab the forst time its clicked on.
If the user then goes back to tabs 1,2,3 and selects more items, the table in the "Cart" tab does not get updated.
Im not sure why this is the case. I have programmed in the "Cell for Row # IndexPath" method to get data from the Datasource each time this method is called.
Im puzzled. Wondering if there is something obvious that Im missing here?
Thanks for your help
The table only updates its data in two situations.
You're using an 'NSFetchedResultsController' to bridge the gap from your data to the view. This controller automatically updates the view whenever the underlying data changes (and, similarly, automatically updates the data when it receives an add/edit/delete request from the view).
You handle it all manually in your own code, most importantly calling the UITableView's reloadData method after the data in the datasource has changed.
you have to reload the data in the table view friend .
[yourtable reloaddata];
use this where you want to show your data

UITableViewController -> EditView - data flow

I have a UITableViewController and in the didSelectRowAtIndexPath method I create an instance of a UIViewController and push it on to the stack.
The UIViewController is meant to edit the content of the selected cell but how do I get the changes made in the ViewController transfered back to the TableViewController?
Cheers
The standard way to pass data back up the hierarchy is with delegation or even more simply through querying a property on the edit view controller when you want to get the edited data back.
Now, when it comes to edit view controllers you can design them to support both direct editing of 'live' objects, or a copy which will let you do a Save/Cancel model.
So what you want is for your edit view controller to edit some sort of ModelObject instance with various properties. These properties will correspond to the textfields or date-pickers etc. in the view. So you might have a Person with NSDate *dateOfBirth and NSString *name.
When you create the view controller and push it onto the navigation controller, you give it one of these ModelObjects to edit. You can either pass in an object straight from your model which will be edited 'live' as the user enters values, or a copy which will let you discard changes and implement a Save or Cancel workflow. For the latter you can add the Save and Cancel buttons yourself before you push the edit view which lets you handle the actions yourself without needing delegation.
So your edit view controller will set the properties on this object when the user enters a new value in a textfield or changes the date picker. For a live object these changes will be applied immediately to your model. For a copy, when the user hits Save you query the edit view controller for the object you passed in and merge/copy that back into your model. If the user hits cancel, you just discard the object.
Override the "parent" or table view controller's -viewWillAppear: method and reload the table view data there, using [tableView reloadData];.
When you go back to the table view controller from the edit view controller, the table view controller's -viewWillAppear: method reloads the data, which in turn calls the table view delegate methods.
You have to make sure that the two controllers 'share' data somehow. The easiest option is to have the first controller pass in the array (or just the selected object) that is selected in the table. The second view would then directly modify that array (or single object) and then when you dismiss the second view will show the modified data.
You might have to reload the table.
Makes sense?