Data Pass to UIPickerView inside of UITableViewCell - iphone

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.

Related

Display current index of iCarousel in a label which is not a subView in ICarousel

I am using iCarousel in my application,I need to get the current index of the iCarousel and display that index in a label(subView of self.view) which is not a subView of iCarousel.
I am able to get the current index With
int index=iCarousel.currentIndex;
How can i update the index in label.text that every time when the carousel is Scrolled.
In which method i have to write the code to update label.
Whenever you scroll the following delegate will called, so you can update your label here
- (void)carouselCurrentItemIndexUpdated:(iCarousel *)carousel1
{
int index=carousel1.currentIndex;
yourlabel.text = [NSString stringWithFormat:#"%d",index];
}
For using the iCarousel, it is better to study its documentation thoroughly before going into its implementation from iCarousel Documentation.
So for the issue you are facing , there are 2 methods provided in the documentation -
- (NSInteger)indexOfItemView:(UIView *)view;
The index for a given item view in the carousel. Works for item views
and placeholder views, however placeholder view indexes do not match
the ones used by the dataSource and may be negative (see
indexesForVisibleItems property above for more details). This method
only works for visible item views and will return NSNotFound for views
that are not currently loaded. For a list of all currently loaded
views, use the visibleItemViews property.
- (NSInteger)indexOfItemViewOrSubview:(UIView *)view
This method gives you the item index of either the view passed or the
view containing the view passed as a parameter. It works by walking up
the view hierarchy starting with the view passed until it finds an
item view and returns its index within the carousel. If no
currently-loaded item view is found, it returns NSNotFound. This
method is extremely useful for handling events on controls embedded
within an item view. This allows you to bind all your item controls to
a single action method on your view controller, and then work out
which item the control that triggered the action was related to. You
can see an example of this technique in the Controls Demo example
project.
Hope this helps !! :)
A bit late, but for others, the following delegate method works for me (with multiple carousels)
- (void)carouselCurrentItemIndexDidChange:(iCarousel *)carousel
{
NSLog(#"%s", __FUNCTION__);
if (carousel == carousel2) {
NSLog(#"here");
long (or int) currentIndex = carousel.currentItemIndex;
}
}

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.

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

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];
}

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];
}

How To Set Nav Bar Item Title From plist's index row?

My data is sourced in a plist and displayed in a tableview with navigation bar.
I want to load the subview with the cell that was clicked.
I know this would set it correct, but I'm not sure how to implement indexPath in the viewDidLoad method?
self.navigationItem.title = [[self.exerciseArray objectAtIndex:indexPath.row]objectForKey: #"name"];
You have an array of Dictionary With you. Now IndexPath is seen called in TableView delegate methods. I doesn't mean that you can't create one. Create an NSIndexpath obect and assign the indexes parameters and send it to the function which contains the above code( separate the code which sets the title from the Cell for row at index path).
But the problem is as I see, You want to show only a specific set of data in dictionary which is stored in an Array. The real problem is You should know which the index of the Array you want to load. to the title and the tableview. If you can know that then its solved.
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSIndexPath_Class/Reference/Reference.html
Regards ,
Jackson Sunny Rodrigues