我有一个 UIPickerView 和一个数组,但我似乎无法将数组中的所有日期输入到 UIPicker 中。
我知道语法应该是这样的:
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [treatments objectAtIndex:row];
}
Treatments 是数组名称,但当我使用它时,它会出现以下错误:
-[Treatment isEqualToString:]: unrecognized
我搜索了整个项目,但找不到短语:isEqualToString
Treatment.h 文件:
#import <Foundation/Foundation.h>
@interface Treatment : NSObject {
NSString *treatmentid;
NSString *treatmentName;
NSString *treatmentPrice;
}
@property (nonatomic, strong) NSString *treatmentid;
@property (nonatomic, strong) NSString *treatmentName;
@property (nonatomic, strong) NSString *treatmentPrice;
@end
所有选择器代码:
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [treatments count];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [treatments objectAtIndex:row];
}
如果您需要更多代码,请说
提前致谢
最佳答案
如果您的数组包含 Dictionar 或 NSMutableDictionar,那么您必须使用其键指定数组的值,如下所示尝试编写如下方法:
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return[[treatments objectAtIndex:row]valueForKey:@"YourKey"];
}
这是选取器 View 示例代码的基本实现:-
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thepickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thepickerView numberOfRowsInComponent:(NSInteger)component
{
return [treatments count];
}
自定义 UIpickerview 的显示标签,如下所示:-
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 37)];
if (component == 0) {
label.font=[UIFont boldSystemFontOfSize:22];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.text = [NSString stringWithFormat:@"%d", row];
label.font=[UIFont boldSystemFontOfSize:22];
NSLog(@"%@",[yourpickerview selectedRowInComponent:component]);
}
return label;
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSLog(@"%@",[treatments objectAtIndex:row]);
}
关于iphone - 如何让我的 UIPickerView 显示数组的所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17760671/