我正在尝试根据视图中标签的值显示具有选定值的 UIPickerView。
例如,如果标签值为 2,并且当我打开 pickerView 时,我希望在选取器中选择值 2。这将如何发生?
我的代码:
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
//res is array
return [[res objectAtIndex:row]objectForKey:@"choice_name"];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
//choiceList is sting
self.choiceList = [NSString stringWithFormat:@"<r_PM act='closepos_choice' cl_choice='%d'/>", row];
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel *pickerLabel = (UILabel *)view;
if (pickerLabel == nil) {
CGRect frame = CGRectMake(0.0, 0.0, 300, 30);
pickerLabel = [[[UILabel alloc] initWithFrame:frame] autorelease];
[pickerLabel setTextAlignment:UITextAlignmentCenter];
[pickerLabel setBackgroundColor:[UIColor clearColor]];
[pickerLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:16.0]];
//EDITED CODE FOR C_X
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * myNumber = [f numberFromString:lblOtomatikPozKap.text];
[f release];
NSInteger index = [res indexOfObject:myNumber];
if(index != NSNotFound ){
[self.pickerView selectRow:index inComponent:0 animated:NO];
}
//EDITED FOR AUSTIN
NSInteger indexOfItem = [res indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
return [[obj objectForKey:@"choice_name"] isEqualToString:@"2"];
}];
if (indexOfItem != NSNotFound)
{
[self.pickerView selectRow:indexOfItem inComponent:1 animated:NO];
}
}
NSString *str = [NSString stringWithFormat:@"%d- %@", row, [[res objectAtIndex:row]objectForKey:@"choice_name"]];
[pickerLabel setText:str];
return pickerLabel;
}
//Not working at all
- (void)selectRow:(UIPickerView*)row inComponent:(NSInteger)component animated:(BOOL)animated
{}