Change UiPickerView values according to user's input - iphone

I want to do somenthing with the UIPickerView but I simple can't see how I can solve this problem...
This example is similar to what I want, and easily understandable. I have two simple pickers, with numbers from 0 to 10. Then I have a textfield where the user inserts a number.
What I want is that when the users inserts, for example, 7, and then selects a number in one of the pickers, like 5, the other pickers moves to 2, so that the sum is 7. The same with 0 and 7, 3 and 4.
Basically, how to make a picker move, according to the other picker value selected, based on the input inserted before.
Thank you very much :)

u add array and reload component like this:
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (component == 0) {
club=[[NSString alloc] initWithFormat:#"%#" , [Nations objectAtIndex:row]];
[secondcomponearray addObject club];
[pickerView reloadComponent:1];
}

try it out suppose you have two UIPickerView like pickerview1 and pickerview2 then you can set the logic like bellow..
Note 1.:- If you used different 2 UIPickerView then use bellow code..
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (thePickerView == pickerview1) {
if ([yourTextfield.text integerValue] > row) {
int selectVal = [yourTextfield.text integerValue] - row ;
[pickerview2 selectRow:selectVal inComponent:0 animated:YES];
}
else{
int selectVal = row - [yourTextfield.text integerValue] ;
[pickerview2 selectRow:selectVal inComponent:0 animated:YES];
}
}
}
Here what you get just see here if you entered value in UITextField 7 and then select value 5 of pickerview1 then its row number is 4.
here when you minus that 4 from the 7 then what you get is 3 after you set or Select the row number is 3 of pickerview2 and its value is 2.
Note 2.:- If you used 1 UIPickerView and 2 components then use bellow code..
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (component == 0) {
if ([yourTextfield.text integerValue] > row) {
int selectVal = [yourTextfield.text integerValue] - row ;
[thePickerView selectRow:selectVal inComponent:1 animated:YES];
}
else{
int selectVal = row - [yourTextfield.text integerValue] ;
[thePickerView selectRow:selectVal inComponent:1 animated:YES];
}
}
}
So you got exact output which you want..
i hope you got it...
Enjoy the coding.. :)

Code modified from this post: find pair of numbers in array that add to given sum
The code below loops through to find the possible pairs adding up to the number (the number typed into the textfield), and then in the picker selects the rows with the numbers equalling one of the numbers in the pair. The code assumes that your data source contains the numbers from 0 up to some total in ascending order.
NSInteger number = [[textField text] integerValue];
int i = 0;
int j = number-1
NSMutableArray *possiblePairs = [NSMutableArray array];
while(i < j){
if (i + j == number) {
possiblePairs[] = #[(i), #(j)];
}
else if (a[i] + a[j] < number) i += 1;
else if (a[i] + a[j] > number) j -= 1;
}
[picker1 selectRow:[dataSource indexOfObject:#(i)] inComponent:0];
[picker2 selectRow:[otherDataSource indexOfObject:#(j)] inComponent:0];

Related

uitableview numberOfRowsInSection count

This is more of a math problem than anything else. What I have is a dynamic array object in which I store user photos.
arryData = [[NSArray alloc] initWithObjects:#"pic1.png", #"pic2.png", #"pic3.png", #"pic4.png", #"pic5.png", #"pic6.png",#"pic7.png", #"pic8.png",nil];
This array can have any amount of objects in it e.g 8 or 20 or 100. In my table view I have created 4 UIImageViews per row by adding them to cell.contentview. So if lets say
if arryData has 3 objects then I want UITable to create 1 row
if arryData has 4 objects then I want UITable to create 1 row
if arryData has 5 objects then I want UITable to create 2 rows
if arryData has 8 objects then I want UITable to create 2 rows
if arryData has 10 objects then I want UITable to create 3 rows
.... and so on
So how do I do this for N number of objects in my arryData?
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//NSLog(#"Inside numberOfRowsInSection");
//return [arryData count];
//Cannot think of a logic to use here? I thought about dividing [arryData count]/4 but that will give me fractions
}
Picture is worth a thousand words.
So basically you need to divide by four, rounding up. Since integer division in Objective-C truncates (rounds toward zero), you can round up by doing this:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return (arryData.count + 3) / 4;
}
In general, to make integer division (with positive integers) round up, you add the denominator-1 to the numerator before dividing.
If you have defined a constant for the number of images per row, use it. For example:
static const int kImagesPerRow = 4;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return (arryData.count + kImagesPerRow - 1) / kImagesPerRow;
}
For the row count, divide by the number of images and round up:
rowCount = ceilf([arryData count] / 4.0);
I suppose you have only one section so:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSInteger photoNumber = [yourDataSource count];
if(photoNumber % numberOfPhotosOnRow == 0) {
return photoNumber/numberOfPhotosOnRow;
}
else {
return photoNumber/numberOfPhotosOnRow + 1;
}
}
I had to create something similar for an application I wrote a little while back(it seems you want to include 4 images per cell)`
if (tableView == yourTableView)
{
int rows = [yourArray count];
int rowsToReturn = rows / 4;
int remainder = rows % 4;
if (rows == 0) {
return 0;
}
if (rowsToReturn >0)
{
if (remainder >0)
{
return rowsToReturn + 1;
}
return rowsToReturn ;
}
else
return 1;
}`

Loading more rows in the tableview

I need to load 15 records initially and then when the user clicks on the show more row of the cell, i need to load the next 20 or available records in my tableview. I am looking at this question posted in SO.
This functionality can be seen in appStore too. Where you load the next number of records.
Here the solution is;
`- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.numberOfRowsVisible;
}
-(void)moreButtonClickAction {
if (self.numberOfRowsVisible < self.maxNumberOfRows)
self.numberOfRowsVisible = MIN(self.numberOfRowsVisible + 10, self.maxRows);
[self.tableView reloadData];
}
I initialized and hard coded the value in the viewDidLoad for numberOfRowsVisible as 15.
self.numberOfRowsVisible=[NSNumber numberWithInt:15];
in numberOfRowsInSection - i am not sure if this is correct.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return MIN([self.arrayRecords count], 15);
}
in the moreButtonClickAction
-(void)moreButtonClickAction {
if (self.numberOfRowsVisible < [self.arrayRecords count])
self.numberOfRowsVisible = MIN(self.numberOfRowsVisible + 15, [self.arrayRecords count]);
[self.tableView reloadData];
}
This is how i edited my code to suit the above suggestion left in SO. but this is not working. There seems to be some issue with MIN. I am not in MAC at the moment, so i can't remember the exact warning/error i got. So could someone kindly help me out.
1) Don't use an NSNumber object, a plain integer will do.
2) Why not just have an integer variable that shows the current number of rows that you want to display, and initialize with 15, and keep it in your class instance.
3) You don't even have to use MIN:
return [self.arrayRecords count] < self.numberOfRowsVisible ? [self.arrayRecords count] : self.numberOfRowsVisible;
You have to use MAX instead of MIN if you want to show all possible entries.
numberOfRowsInSection should return an integer.
self.numberOfRowsVisible is not an integer.
viewDidLoad:
self.numberOfRowsVisible=15;
main implementation:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.numberOfRowsVisible;
}
-(void)moreButtonClickAction {
if (self.numberOfRowsVisible < [self.arrayRecords count]) {
self.numberOfRowsVisible += 15;
if (self.numberOfRowsVisible > [self.arrayRecords count]) {
self.numberOfRowsVisible = [self.arrayRecords count];
}
}
[self.tableView reloadData];
}
off the top of my head, code could do with re-factoring.

How to place the morethan one pickerviews in view?

Hai,In my app i want to place the 4 pickerviews.And i place the one segment control with 3 segments.If we click on first segment then firstpicker will be display.Likewise remaining pickers are display for remaining segments.But,only first picker will be displayed and remaining pickers are not show components and rows.Only display black screen.Please provide any help.Please urgent.
To again call the delegates, call the following line
[pickerview reloadComponent];
create outlet and make use of hidden property...means if you tap on first segment make first picker as highlighted and similarly for other segments with respective pickers...
do you want to resize ??
Edit: added code from below for formatting purposes
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return ( pickerView == picker1 ? 2 : 3 );
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { NSArray *values = ( pickerView == picker1 ? values1 : values2 );
return [values count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSArray *values = ( pickerView == picker1 ? values1 : values2 );
return [values objectAtIndex: row];
}
check like this..

picker view - units conversion

I'm developing an application with a picker view that slide from the bottom (like an action sheet). I need to convert 3 units (litres, us gallons and imperial gallons). The value is taken from a text field but I don't know how to tell the compiler which was the unit to start from.
(ie...the user insert the value in the the text field but if he changes the units (usg or ig), the value in the text field changes accordingly.
this is the array:
NSMutableArray *weightArray = [[NSMutableArray alloc] initWithObjects:#"", #"Ltr",#"Usg",#"Ig",
nil];
self.weightPickerViewArray = weightArray;
[weightArray release];`
this is the picker:
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (row == 1) {
labelVolume.text = [weightPickerViewArray objectAtIndex:row];
//more code here....
}else if (row == 2){
labelVolume.text = [weightPickerViewArray objectAtIndex:row];
//more code here....
}else if (row == 3){
labelVolume.text = [weightPickerViewArray objectAtIndex:row];
//more code here....
}
how can I tell the compiler which was the value before the selection?
Add a text indicating the units at the end of the string.
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (row == 1) {
NSString *initalValue = labelVolume.text;
// Do your calculations here based on unit
NSString *updatedValue = urNewValue;
labelVolume.text = updatedValue;
//more code here....
}else if (row == 2){
NSString *initalValue = labelVolume.text;
// Do your calculations here based on unit
NSString *updatedValue = urNewValue;
labelVolume.text = updatedValue;
//more code here....
}else if (row == 3){
NSString *initalValue = labelVolume.text;
// Do your calculations here based on unit
NSString *updatedValue = urNewValue;
labelVolume.text = updatedValue;
//more code here....
}
When you select a row in picker, it call's its delegate method pickerView:didSelectRow:inComponent:
Here, you can can change the value of your text field depending on which row (liters, gallons, etc) user selected.
Please refer the apple's class reference for UIPickerView delegate
If I understand your problem. You want catch event when user change units in your pickerView, pass the information to the view that contains text field and execute conversion.
So you can use NSNotificationCenter (documentation here). In your pickerView when value change put this code :
[[NSNotificationCenter defaultCenter] postNotificationName:#"unitValueChange" object:#"litres"];
In the view that contains UITextField put this code :
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMethodToExecute:) name:#"unitValueChange" object:self];
Now when your PickerView launch notification your view launch this method :
-(void)myMethodToExecute:(NSNotification *)notif{
NSString *myUnitSelected = notif.object
}

How can I get the row number of each components in UIPickerView

How can I get the row number of each component in UIPickerView?
I Have UIPickerView Whit 3 components:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
And The second question is How can I change width of each component?
You're probably populating the UIPickerView from an array; use the row parameter in a call to
[dataArray objectAtIndex:row];
to get the data. If you need to know the total number of rows, just use [dataArray count]; to get the size of your data source.
EDIT: I forgot that if you're following the protocol, you'll have implemented this method:
-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
NSInteger numRows = 0;
switch (component) {
case 0:
numRows = 6;
break;
case 1:
numRows = 3;
break;
case 2:
numRows = 2;
break;
default:
break;
}
return numRows;
}
So, you're actually telling the system how many rows for each component. Check this, or go ahead and implement it, and you'll have your answer!
EDIT 2: if you want to get the current selected row in each component, use:
[yourPickerView selectedRowInComponent:1];
as many times as you need. I suggest looping through all components and using this method to get the selected row.