Tattat
Tattat

Reputation: 15778

How to optimize the picker view in 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?

Upvotes: 0

Views: 331

Answers (3)

Michael Kessler
Michael Kessler

Reputation: 14235

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

Upvotes: 3

Ole Begemann
Ole Begemann

Reputation: 135558

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

Upvotes: 0

user121301
user121301

Reputation:

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

Upvotes: 0

Related Questions