views:

132

answers:

3

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?

A: 

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

DyingCactus
I want the user can actually choose the number, I see the Apple date picker also can allow user to scroll many items.
Tattat
If the row titles of the picker will be of the form "x unit(s)", you can generate the string in the titleForRow method instead of pre-allocating the array of strings.
DyingCactus
A: 

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

Ole Begemann
Really? But my code take really long.
Tattat
+3  A: 

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")];
}
Michael Kessler
It works fast enough even though each time the `titleForRow` is called new `NSString` object is allocated.
Michael Kessler