How to optimize the picker view in iPhone? - iphone

I want my picker view can display 1 - 200. but I think it is too much memory, if I assign an Array in this view:
self.myLargeView = [[NSArray alloc] initWithObjects:#"1 unit", #"2 units", .... ..., #"199 units" #"200 units", nil]; //code skipped
How can I reduce the memory load in application? any ideas?

If you want your picker view just to display an incremented number then you might use something like this:
#define kPickerValuesAmount 200
#pragma mark -
#pragma mark UIPickerViewDataSource methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return kPickerValuesAmount;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [NSString stringWithFormat:#"%i unit%#", (row + 1), (row == 0 ? #"" : #"s")];
}

If the user has to just pick a number from 1 to 200, why not use a UISlider instead?

An array of 200 short NSStrings will not cause you any memory problems. It just takes up a few kilobytes.

Related

UIPickerView with empty arrays

I've set up a UIPickerView successfully, but I'm wondering about the best way to handle the case when the data is initially empty. For my current iteration I use the following code for the pickerdelegate
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if ([flash_cards count])
{
return [[flash_cards allKeys] objectAtIndex:row];
}
return #"";
}
where
flash_cards = NSMutableDictionary *
Whenever I try to scroll it creates an error of indexoutofbounds which is only logical. How do I handle the case of an empty array?
EDIT:
Also as part of the code I implemented the following
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponen: (NSInteger)component
{
return [flash_cards count];
}
#pragma mark Picker Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
To solve this problem you should return 1 instead 0 in the method numberOfRowsInComponent from the UIPickerViewDataSource when the array is empty, like the example below:
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [self.myArray count] == 0 ? 1 : [self.myArray count];
}
You should also implement the UIPickerViewDataSource methods:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
You can still use flash_cards (or [flash_cards count]) to derive the return values of these methods; if your data is empty, you can return 0, then later call -reloadAllComponents on your picker view when you get data loaded.

Iphone:Split a string seperated by comma and print each in row of pickerview

I got a string as mild,medium,hot.I split the string with comma as separator.I need to print it in a pickerView too.
I used the following code and got List count as 3 successfully.
NSString *spList=[mdict objectForKey:#"spicinesstype"];
NSArray *list = [spList componentsSeparatedByString:#","];
NSLog(#"List count:%d",[list count]);
return [list count];
But how could I display all 3 items in pickerview
You should set your class as the delegate of the picker view, Then implement these 3 delegate methods for your picker
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return [array count];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [array objectAtIndex:row];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
First you need to implement UIPickerViewDelegate and UIPickerViewDataSource by putting them on the end of the #interface line of the header file of your view controller.
Like this: #interface MyViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>
Next you need to set your view controller as the delegate and data source of you picker view. You can either do this in the - (void)viewDidLoad method of your view controller by adding the lines:
myPickerView.delegate = self;
myPickerView.dataSource = self;
Or you can link it up in Interface Builder if you are using that.
Then you need to implement these delegate methods in your View Controller source file.
(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 1;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
// whatever you want to happen when a row is selected.
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
return [list count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
return [list objectAtIndex:row];
}

Can't solve the double picker with images

I'm trying to do a double picker, one part of text and the other with images. But the code gives me an error: Thread 1: Program received signal: "EXC_BAD_ACCESS". I can't see the trouble. Here is the code, Array content Images and Array2 content text. Thanks.
#synthesize Array, picker, Array2;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
UIImage *one = [UIImage imageNamed:#"6-12AM.png"];
UIImageView *oneView = [[UIImageView alloc] initWithImage:one];
NSArray *Array1 = [[NSArray alloc] initWithObjects:oneView, nil];
NSString *fieldName = [[NSString alloc] initWithFormat:#"Array"];
[self setValue:Array1 forKey:fieldName];
Array2 = [[NSArray alloc] initWithObjects:#"Hello", #"trouble", nil];
[super viewDidLoad];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 2;// giving number of components in PickerView
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component;
{
return [self.Array2 count];// counting the number of rows in each component
}
#pragma mark Picker Delegate Methods
- (UIView *)pickerView:(UIPickerView *)pickerView
viewForRow:(NSInteger)row
forComponent:(NSInteger)component
reusingView:(UIView *)view
{
if (component == 1) {
return [self.Array objectAtIndex:row];
}
} //In this line is where the error occurs
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component; {
if (component == 0) {
return [self.Array2 objectAtIndex:row];
}
}
Your error is due to the fact that you do not offer an alternative to the if statement. A non-void function must return something in every case, so you need to provide an else statement in each one of your two functions that returns a value (like nil).
I dont see you allocating or initializing Array. Hence the bad access. Did you mean to use Array1?
Also several things:
Memory allocated several places never deallocated. Leaks all over
Variable names should start with small caps like array1, array2
[self setValue:Array1 forKey:fieldName]; //what is this doing?

My Picker shows question marks, but still sends the proper info to my Label

I made a simple picker that displays the information chose in a label on the same page. The picker is just showing ? ? ?, but when i scroll over the ? it shows the correct things chosen in the label. How do I get it to also show the words in the pickerwheel itself?
Without seeing your code, it is difficult to track down. How are you setting the information in the Label and the Picker?
If you have an array of strings which are also displayed in the Picker, then this is easy:
In the .h file, use:
#interface ViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>{
NSArray * arrayOfString;
UILabel * theLabel;
UIPicker * thePicker;
}
In the .m file, use:
#pragma mark -
#pragma mark View
- (void)viewDidLoad;{
[super viewDidLoad];
arrayOfStrings = [[NSArray alloc] initWithObjects:#"1", #"2", #"etc", nil];
}
#pragma mark -
#pragma mark Picker Delegate Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView; {
return [arrayOfStrings count];
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component; {
return 1;
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component; {
return [arrayOfStrings objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component; {
theLabel.text = [arrayOfStrings objectAtIndex:row];
}
You then hook everything up in the nib, and this should fix your problem. Hope this helps!

How do I get the item selected in the UIPickerView with NSIntegerMax?

I'm new to Objective-C, and I already have a button with a function to go when the press the button. In the UIPickerView it has lots of numbers going up until who knows how long. I used this code:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 3;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
// Near-infinite number of rows.
return NSIntegerMax;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [NSString stringWithFormat:#"%d", row];
}
I just need the number that was entered in all three components.
The value for each component is found in:
[yourPicker selectedRowInComponent:x]
where x is the component number (0, 1, or 2 in your case).