Save data from 1 page and add that data to a tableView - iphone

Hey guys i have 2 views the first has a UITableView. The Second Has a textField and when the user presses a "Save" button on the second page, i want the textFields text to be added to the tableView. Here is the code i'm using
- (IBAction)saveButton:(id)sender {
CheckListPracticeViewController * obj = [[CheckListPracticeViewController alloc]init];
[obj.cells insertObject:textField.text atIndex:0];
[self dismissModalViewControllerAnimated:YES];
NSLog(#"%# "[cells objectAtIndex:0]);
[obj.myTableView reloadData];}
For some reason the data isnt being added to the table View does anybody know whats wrong?? Also the NSLog doesnt Work in this method. Thanks a lot Guys :D

That is because you are creating a new instance of CheckListPracticeViewController and updating it rather than the current one which has presented this view controller modally.
Change the first line to,
CheckListPracticeViewController * obj = (CheckListPracticeViewController *)self.parentViewController;
EDIT
First of all be consistent with your data model. If you are loading an array of dictionaries from the plist and later adding strings into that array then you have a serious problem. I will suggest that you create a dictionary object with name and other stuff and add that to the array. I would say doing [obj.cells insertObject:[NSDictionary dictionaryWithObject:textField.text forKey:#"name"] atIndex:0]; instead of [obj.cells insertObject:textField.text atIndex:0]; will fix this current error but I doubt that will fix your problem.

You are each time the button is pressed you alloc/init a new obj, try adding the new data into your actual container.

It's definitely one of these.
Check if array cells is allocated. Most likely it is not. That is why it is not being added to the tableView. You can add it to the init method so that you initialize the array before adding to the array.
Try NSlogging the contents of the array at various points. This will let you know if its exactly what's going on. NSLog the array in IBAction of Add-method, viewWillAppear
You need to make sure that your array is allocated before adding data to it.
NSLog the content of textField.text before adding it to the array. I am guessing that it is not a property, and it is simply null.
Make sure you had assigned self.myTableView = tableView at the end of your cellForRowAtIndexPath

You are creating another instance of CheckListPracticeViewController. I assume thats where your table is. If you are going back to CheckListPracticeViewController (I assume you do since you use [self dismissModalViewControllerAnimated:YES]) you will have to pass your view controller as a week reference or use NSNotification or use NSUserDefaults to store and retrieve this object in the CheckListPracticeViewController.
EDIT
Pass the CheckListPracticeViewController by weak reference to the view that has UITextField.
example:
in the .h of your UITextField class create a controller reference.
#property(nonatomi,assign)CheckListPracticeViewController *controller;
then when you create your new UITextField controller class pass the reference to its creator throught controller instance.
//in CheckListPracticeViewController.m file
myEdiotr.controller = self;
Later use controller instance to save the text from the UITextField.
- (IBAction)saveButton:(id)sender {
[controller.cells insertObject:textField.text atIndex:0];
[controller.myTableView reloadData];
[self dismissModalViewControllerAnimated:YES];
}

Related

Subclassed UIView doesn't get tag set

I have subclassed a UIView class and I create multiple instances of this class in a loop (incrementing each time), however when I try to set the tag of the view, and log it to the console after they are all created, they all have a tag of 1, regardless of what I set the tag as.
Any help would be appreciated, Thanks!
My Code for creating the subview is here:
//for() loop above with i as counter
FlashCardView *subview = [[FlashCardView alloc] initWithFrame:frame];
subview.delegate = self;
subview.viewNum=i+10; //My attempt at a workaround but I cannot get the view with this later so it is not very helpful
[subview setTag:i+10]; //Tried this and subview.tag=i+10;
NSLog(#"%d", subview.tag); //Prints correctly
//Gets added to parent later
This NSLog logs the correct tag, however when I log the tag in the UIView subclass, it always returns its tag as 1. Also, if I print all the subviews of the parent in a later called method (In the viewcontroller), all of them have the tag 1.
I cannot tell you why, but I can tell you how to find the problem. In your FlashCardView subclass, add this method:
- (void)setTag:(NSInteger)theTag
{
assert(theTag != 1);
[super setTag:theTag];
}
Then, when whatever is setting the tag to 1 does it, the assert will fire and you can look at the stack trace and see where its coming from.
Alternately, remove the assert, and put a breakpoint on the super message.
PS: make sure you enable exceptions!
Hi thanks for all your help, I found that I had accidentally typed in the subclass's variable when I meant to put in something else, setting the tag to 1. #Sunny, thanks for telling me to double-check.

Load Data depending on what TableView Cell was selected

I have a table view. I want my app to work like the "Contacts" app on the iPhone. When you select a person in the app, it goes to a view that has the users name and phone number. and it doesnt matter which cell is selected, it always goes to the same view, but with different data. How can i achieve the same thing? I want one view controller and i want each cell to load certain data onto the presented view controller! And i would like to use NSUserDefaults (if possible). Any Help would be great, Thank You!
P.S. i know this is kind of a broad question, but im unsure where to find an answer i have searched and searched. Thanks again!
I want one view controller and i want each cell to load certain data
onto the presented view controller!
No problem. User taps on cell, your table's delegate gets a -tableView:didSelectRowAtIndexPath: message. Let's say the table's delegate and data source are both the view controller that manages the table (because that's a pretty common setup). So the view controller takes a look at the index path for the cell, grabs the associated data from wherever it keeps its data, instantiates the detail view controller with the data connected to the tapped cell, and pushes it.
The part that you're probably missing is some sort of model that stores the data displayed in the table. The view controller for the table needs to know about the model because it's the data source for the table. It already needs to know where to find enough data to configure each cell in the table. If you use that same model to store the detail data, you'll be able to use it to configure the detail view controller.
Example: Let's say you have a table with just one section. A simple model for that table could be an array of dictionaries. When your table view controller needs to populate a cell, it uses the row of the index path as an index into the array to get a dictionary, and it uses that to set up the cell. Exactly the same thing happens when the user taps a cell: you use the row from the index path to get the right dictionary from the array, and you use that to set up the detail view controller. You could even just pass the whole dictionary to the detail view controller and let the detail view controller get what it needs. That way the table view controller doesn't have to worry about exactly which details the detail view controller displays.
You create an array of names (strings) and display those names in your table view.
Then once a name was selected you save it in NSUserDefaults as a string.
- (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath
{
NSString *selectedString = [namesArray objectAtIndex:[indexPath row]];
NSUserDefaults *infoSaved = [NSUserDefaults standardUserDefaults];
[infoSaved setObject:selectedString forKey:#"theName"];
[infoSaved synchronize];
[self performSegueWithIdentifier:#"segueToMoreInfo" sender:self];
}
Then at your second view controller you load the information from an NSDictionary that contains NSDictionary items.
-(void) loadData {
NSUserDefaults *infoSaved = [NSUserDefaults standardUserDefaults];
NSString *theName = [infoSaved objectForKey:#"theName"];
NSDictionary *currentNameToDisplay = [itemsDictionary objectForKey:theName];
// then you can set your labels with information in that NSDictionary item
[nameLabel setString:[currentNameToDisplay objectForKey:#"userName"];
[ageLabel setString:[currentNameToDisplay objectForKey:#"userAge];
}
That would be a simple basic structure.
You can save the main Names NSDictionary that contains the items into a plist file or even better save the dictionary in NSUserDefaults.
Hope that helps.

Data Pass to UIPickerView inside of UITableViewCell

I have UITableView and there is a picker view inside every cell. User can change value of every picker separately.
I need to know that which cell's pickerView is changed. For example, when user move a picker I will know that, this is cell 3.
Is there any way to do that? It's not necessary to get instant changed values. We can store it on array than send when Go button clicked that is not related with tableview.
I would suggest to store an array of pointers to your picker views. So index 0 of your array would point to the picker view of table row 0, etcetera. Then, make sure your view controller is the delegate of all picker views. Implement the pickerView:didSelectRow:inComponent: method declared by the UIPickerViewDelegate protocol. That method has an instance of UIPickerView as parameter: that's the one which value was changed. To obtain the row of that picker view, you can send your array the indexOfObject: method.
Create a subclass of a UITableViewCell. That subclass should get a reference to whatever you want to update, and then it can simply create (or IBOutlet link) to the picker and be a delegate of the picker.
So Cell should have:
- (void) passStuff:(NSMutableDictionary *)datamodel key:(NSString *)key values:(NSArray *)values {
_privateWeakRefDataModel = datamodel;
_privateCopyKey = key;
_privateValues = values;
[picker reloadData];
}
#pragma mark - Picker Delegate Code here
This way the cells will handle updating your data model directly. Keeps your code much cleaner.

How to Update UIScrollView when an index of array is Deleted

i am new to IPhone Development. can some one tell me how to update the content of UIScrollview. i have an NSMutable Array that contains different quotes so if i delete one of the quotes from the scrollview how it can be remove from view and page should move to the next page if the middle one is deleted and should go to the previous one if the last index of an array is deleted. Please Help!
Thanks in Advance
The simplest approach is to create two methods - for adding and removing objects from the NSMutableArray and inside them change the contents of the UIScrollView. For example:
-(void)addObjectToArray:(id)obj
{
[self.myArray addObject:obj];
//Create and initialize a view with a quote for example and add it in the UIScrollView
[self.scrollView addSubView:someView];
}
-(void)removeObjectFromArray:(id)obj
{
[self.myArray removeObject:obj];
//Find the view you need to remove, either by tag or your array is actually from views,
//and remove it from the UIScrollView
[someView removeFromSuperview];
}
Then when inserting or deleting objects from the array use those methods
[self addObjectToArray:someObject];
[self removeObjectFromArray:someObject];
There are some other more elegant approaches using KVO, but need to read some info about that..
after deleting image remove that data from array
[dataarray removeObjectAtIndex:selectedindex];
now again call the scrollview method

Add data to a table View [duplicate]

Hey guys i have 2 views the first has a UITableView. The Second Has a textField and when the user presses a "Save" button on the second page, i want the textFields text to be added to the tableView. Here is the code i'm using
- (IBAction)saveButton:(id)sender {
CheckListPracticeViewController * obj = [[CheckListPracticeViewController alloc]init];
[obj.cells insertObject:textField.text atIndex:0];
[self dismissModalViewControllerAnimated:YES];
NSLog(#"%# "[cells objectAtIndex:0]);
[obj.myTableView reloadData];}
For some reason the data isnt being added to the table View does anybody know whats wrong?? Also the NSLog doesnt Work in this method. Thanks a lot Guys :D
That is because you are creating a new instance of CheckListPracticeViewController and updating it rather than the current one which has presented this view controller modally.
Change the first line to,
CheckListPracticeViewController * obj = (CheckListPracticeViewController *)self.parentViewController;
EDIT
First of all be consistent with your data model. If you are loading an array of dictionaries from the plist and later adding strings into that array then you have a serious problem. I will suggest that you create a dictionary object with name and other stuff and add that to the array. I would say doing [obj.cells insertObject:[NSDictionary dictionaryWithObject:textField.text forKey:#"name"] atIndex:0]; instead of [obj.cells insertObject:textField.text atIndex:0]; will fix this current error but I doubt that will fix your problem.
You are each time the button is pressed you alloc/init a new obj, try adding the new data into your actual container.
It's definitely one of these.
Check if array cells is allocated. Most likely it is not. That is why it is not being added to the tableView. You can add it to the init method so that you initialize the array before adding to the array.
Try NSlogging the contents of the array at various points. This will let you know if its exactly what's going on. NSLog the array in IBAction of Add-method, viewWillAppear
You need to make sure that your array is allocated before adding data to it.
NSLog the content of textField.text before adding it to the array. I am guessing that it is not a property, and it is simply null.
Make sure you had assigned self.myTableView = tableView at the end of your cellForRowAtIndexPath
You are creating another instance of CheckListPracticeViewController. I assume thats where your table is. If you are going back to CheckListPracticeViewController (I assume you do since you use [self dismissModalViewControllerAnimated:YES]) you will have to pass your view controller as a week reference or use NSNotification or use NSUserDefaults to store and retrieve this object in the CheckListPracticeViewController.
EDIT
Pass the CheckListPracticeViewController by weak reference to the view that has UITextField.
example:
in the .h of your UITextField class create a controller reference.
#property(nonatomi,assign)CheckListPracticeViewController *controller;
then when you create your new UITextField controller class pass the reference to its creator throught controller instance.
//in CheckListPracticeViewController.m file
myEdiotr.controller = self;
Later use controller instance to save the text from the UITextField.
- (IBAction)saveButton:(id)sender {
[controller.cells insertObject:textField.text atIndex:0];
[controller.myTableView reloadData];
[self dismissModalViewControllerAnimated:YES];
}