任何方式显示1/2而不是.5在UIPickerView?

时间:2011-04-19 10:13:26

标签: iphone objective-c cocoa-touch

是否可以在UI PickerView中显示1/2而不是.5?

这是我目前使用的代码:

pickerArray = [[NSMutableArray alloc] initWithCapacity:700];
for ( float i = 0.0 ; i <= 1000.0 ; i = i + 2.5)
{
    //[pickerArray addObject:[NSString stringWithFormat:@"%.1f", i]]
    [pickerArray addObject:[NSNumber numberWithFloat:i];
}

float weight = [[pickerArray objectAtIndex:row] floatValue];
label.text = [NSString stringWithFormat:@"%.1f", weight];

1 个答案:

答案 0 :(得分:2)

我建议做这样的事情:

NSMutableArray *array = [[NSMutableArray alloc] init];
for (int i=0;i<100;i++) {
    float value = (float)i*2.5;
    if (i % 2) {
        [array addObject:[NSString stringWithFormat:@"%d %@",(int)value,@"1/2"]];
    } else {
        [array addObject:[NSString stringWithFormat:@"%d",(int)value]];
    }
}

数组包含:

"0", "2 1/2", "5", "7 1/2", "10", "12 1/2", "15", "17 1/2", "20", "22 1/2", "25", "27 1/2", "30", "32 1/2", "35", "37 1/2", "40", and more

像这样更新标签:

label.text = [array objectAtIndex:row];