Most effective way to populate a picker view with range of integers? - iphone

I have a simple UI picker view in an iOS app (iPhone) and I'm looking to pre-populate it with a range of numbers on launch. What would be the most pragmatic/quickest/optimized way to populate it? I'm new to iOS development, so I'm just starting to test the waters. The documentation is pretty decent but I'd like to get some insight from seasoned developers on the most effective way to accomplish what I'm doing?
tl;dr
I want to populate a UI picker view with the the number range 45-550 upon the start of my application, what's the best way to do so?

Despite the possibility that I may not qualify as a seasoned developer, I would do it this way. In the class that will be the data source and delegate of the picker view:
#define PICKER_MIN 45
#define PICKER_MAX 550
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return (PICKER_MAX-PICKER_MIN+1);
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [NSString stringWithFormat:#"%d", (row+PICKER_MIN)];
}

Related

How to change the size of the characters inside a UIPicker

I am trying to center and change the size of the text inside my UIPicker but am not sure how... I think it might happen in one of these methods, most likely the first...
- (NSString*)pickerView:(UIPickerView*)pv titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [[NSString stringWithFormat:#"%x",row ] uppercaseString]; //Sets picker options as an Uppercase Hex value
}
#pragma mark UIPickerViewDataSource methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pv
{
return 5; //5 columns in the picker
}
- (NSInteger)pickerView:(UIPickerView*)pv numberOfRowsInComponent:(NSInteger)component
{
return 16;
}
Dose anyone one know how this can be done? I have looked around but the documentation is fairly thin...
Johns,
As in Table Views, you can't change the font attributes of the default title that you usually set in the titleForRow delegate method, instead you have to use the following method:
- (UIView *)viewForRow:(NSInteger)row forComponent:(NSInteger)component
Here you can create an UIView with a custom UILabel and set the Label's font size, color and other attributes.
This method is described in Apple's UIPickerView Class Reference
Hope this help you!
Now with iOS 6 you can use NSAttributedStrings to customize the title text (though I don't think this lets you easily center the text).
A new UIPickerViewDelegate method lets you return an attributed string instead of a plain vanilla string:
(NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
The NSAttributedString UIKit additions page has a list of all the character attributes you can control:
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/NSAttributedString_UIKit_Additions/Reference/Reference.html#//apple_ref/doc/uid/TP40011688

instantiate UIPicker with uitextfield string

I am trying to pass my UItextfield value over to a UIPicker that I have loading in a alertview I'm just not sure what functions are available to me to get the values back into the picker?
At a guess I should be doing something in my
- (NSInteger)pickerView:(UIPickerView*)pv numberOfRowsInComponent:(NSInteger)component
{
if (pv == pickerViewA) {
return 10;
}
else
return 16;
}
but looking at the developers notes their is not much info about the UIPickerViewDataSource other than returning number of rows...
any help would be greatly appreciated.
There are other data source and delegates as-
//for title of a row in a component
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
//for width of a componenet
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
//for row height of a component
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
//for number of components
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
for more info look at documentation -
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIPickerViewDelegate_Protocol/Reference/UIPickerViewDelegate.html
and
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIPickerView_Class/Reference/UIPickerView.html
You can use either one of the following two delegates to add data content to UIPickerView
– pickerView:titleForRow:forComponent:
– pickerView:viewForRow:forComponent:reusingView:
For the first one u can return any NSString object and you cannot customize anything like font size or color for that.
But if you use the second one you can add the data content as a view to UIPickerView which can be achieved using a UILabel and here you can customize the view as you require.
Hope this might help you
Happy coding !

Fill UIPickerView with an Array Value

i am working on a project in which i have to perform following two work:
1.) Fetch value from CoreData and store it in an NSMutableArray.
2.) Take a UIPickerView and fill it with an Array value.
Problem is that size of array is dynamic and i canto fill an array value in UIPickerView. can someone help me.
in order for the UIPickerView to work correctly, you must supply the amount of components (columns) and the number of rows for each component whenever it reloads:
as mentioned, use [myPickerView reloadAllComponents]; to reload the view once the array is populated, but you MUST implement also these things after you declare the containing view controller class as <UIPickerViewDelegate> link the picker to the file owner as a delegate, and then:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;// or the number of vertical "columns" the picker will show...
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (myLoadedArray!=nil) {
return [myLoadedArray count];//this will tell the picker how many rows it has - in this case, the size of your loaded array...
}
return 0;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
//you can also write code here to descide what data to return depending on the component ("column")
if (myLoadedArray!=nil) {
return [myLoadedArray objectAtIndex:row];//assuming the array contains strings..
}
return #"";//or nil, depending how protective you are
}
After updating the value (Inserting or modifying existing) in your array.
Call reloadAllComponents on your UIPickerView instance.
- (void)reloadAllComponents
Use as below
[myPickerView reloadAllComponents];

In iphone can i add one picker view on two text boxes /uilabel with diffrent values?

In my project i am filling the picker view array with the value which i am getting from the service.And in the same view i have another label on click of button in the label i want to get the same uipickerview with different value is that possible.....so with one picker view my work will be done no need to call to different picker views...anyone has solution for this isssue
i dont think so .. it will only be possible if you load the data again which means the uipicker(all same instance) with loaded with same data
of course this is possible. Just switch to another model that holds the data that is used in the picker.
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
if (myCurrentEditingValue == MyCountryValue) {
return ...
}
else if (myCurrentEditingValue == MyCityValue) {
return ...
}
return nil;
}
- (IBAction)startEditingCountryField {
myCurrentEditingValue = MyCountryValue;
[picker reloadAllComponents];
}
You should get the idea.

Can we enable looping of rows in a UIPickerView as we do it in UIDatePicker? [duplicate]

This question already has answers here:
How do you make an UIPickerView component wrap around?
(3 answers)
Closed 9 years ago.
I am developing an App where i am struck with the looping of rows in a UIPickerView. Can anyone please help me? it would be of great help if anyone would post the solution. I want the rows in a UIPickerView scroll continuosly in a circular manner without having a end point.
It is possible. For anyone of you who needs it,
try this.
I don't think it's possible. I've heard of people repeating the list of values a large number of times, and starting the user off somewhere in the middle.
It does work, just watch the memory. Told that items not shown are not stored so can make list huge. Check with profiler if worried.
It's just as easy to set the number of rows to a large number, and make it start at a high value, there's little chance that the user will ever scroll the wheel for a very long time -- And even then, the worse that will happen is that they'll hit the bottom.
(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
// Near-infinite number of rows. use NSIntegerMax, if memory problem, use less say 2000
return 2000;
}
(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
// Row n is same as row (n modulo numberItems).
return [NSString stringWithFormat:#"%d", row % numberItems]; // or your strings (this is for double. numberItems is your list size.
}
(void)viewDidLoad {
[super viewDidLoad];
self.pickerView = [[[UIPickerView alloc] initWithFrame:CGRectZero] autorelease];
// ...set pickerView properties... Look at Apple's UICatalog sample code for a good example.
// Set current row to a large value (adjusted to current value if needed).
[pickerView selectRow:3+1000 inComponent:0 animated:NO]; //pick about half the max you made earlier or about 100000 if using NSIntegerMax
[self.view addSubview:pickerView];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSInteger actualRow = row % numberItems; //nb numberItems is your list size
// ...
}
Jon