我一直在阅读拾取器,想尝试一个简单的工作,但我不知道我的代码出错了。
我已将我的故事板中的拣货员分配给正确的控制器。然后我将以下内容添加到我的标题中。
@interface CustomerView : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>
{
IBOutlet UIPickerView * CompanyPicker;
IBOutlet UIPickerView * ClientPicker;
NSMutableArray *arrayCompanies;
NSMutableArray *arrayClients;
}
然后我将一些信息添加到我在didload下的标题中创建的数组中:
arrayCompanies = [[NSMutableArray alloc] init];
[arrayCompanies addObject:@"Test Company 01"];
[arrayCompanies addObject:@"Test Company 02"];
[arrayCompanies addObject:@"Test Company 03"];
[arrayCompanies addObject:@"Test Company 04"];
[arrayCompanies addObject:@"Test Company 05"];
[arrayCompanies addObject:@"Test Company 06"];
[arrayCompanies addObject:@"Test Company 07"];
arrayClients = [[NSMutableArray alloc] init];
[arrayClients addObject:@"Test Client 01"];
[arrayClients addObject:@"Test Client 01"];
[arrayClients addObject:@"Test Client 01"];
[arrayClients addObject:@"Test Client 01"];
[arrayClients addObject:@"Test Client 01"];
[arrayClients addObject:@"Test Client 01"];
[arrayClients addObject:@"Test Client 01"];
之后我在实现中添加了以下内容,但信息似乎不会加载到我的数组中。我在这里做错了吗?
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return [arrayCompanies count];
return [arrayClients count];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [arrayCompanies objectAtIndex:row];
return [arrayClients objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSLog(@"Selected Company: %@. Index of selected company: %i", [arrayCompanies objectAtIndex:row], row);
NSLog(@"Selected Client: %@. Index of selected client: %i", [arrayClients objectAtIndex:row], row);
}
答案 0 :(得分:2)
使用这种技术:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
if ([thePickerView isEqual: CompanyPicker]) {
return [arrayCompanies count];
} else if ([thePickerView isEqual: ClientPicker]) {
return [arrayClients count];
}
return 0;
}