【发布时间】:2020-07-21 01:12:11
【问题描述】:
我在 UIPickerView 中重新加载组件时遇到问题。这是我的代码:
在我的头文件 (.h) 上:
@interface GoToAyatViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>{
IBOutlet UIPickerView *pickerViewObj;
}
@property (nonatomic, retain) IBOutlet UIPickerView *pickerViewObj;
@end
关于我的实现 (.m):
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
ratusan = [[NSMutableArray alloc] initWithObjects:@" 0",@" 1",nil];
puluhan = [[NSMutableArray alloc] initWithObjects:@" 0",@" 1",@" 2",
@" 3",@" 4", @" 5",
@" 6", @" 7", @" 8",
@" 9",nil];
satuan = [[NSMutableArray alloc] initWithObjects:@" 0",@" 1",@" 2",
@" 3",@" 4", @" 5",
@" 6", @" 7", @" 8",
@" 9",nil];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
pickerViewObj = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 250, 400, 80)];
pickerViewObj.showsSelectionIndicator = YES;
pickerViewObj.dataSource = self;
pickerViewObj.delegate = self;
[self.pickerViewObj selectRow:2 inComponent:0 animated:NO];
[pasalTypeButton setBackgroundColor:[UIColor blueColor]];
self.isPasal = YES;
self.title = self.kitab;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return kPICKERCOLUMN;
NSLog(@"numberOfComponentsInPickerView jalan");
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
NSLog(@"numberOfRowsInComponent jalan");
if (component==0){
return [ratusan count];
}else if (component == 1) {
return [puluhan count];
}else{
return [satuan count];
}
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSLog(@"set title run");
switch (component)
{
case 0:
return [ratusan objectAtIndex:row];
//break;
case 1:
return [puluhan objectAtIndex:row];
//break;
case 2:
return [satuan objectAtIndex:row];
//break;
}
return nil;
}
在实现中,我使用 ibaction 为我的 uipickerview 上的第一个组件设置新值并触发 reloadallcomponents:
-(IBAction) showAyat
{
self.ratusan = nil;
self.ratusan = [[NSMutableArray alloc] initWithObjects:@" 0",@" 1",@" 2",nil];
[self.pickerViewObj reloadAllComponents];
NSLog(@"total ratusan=%d", [self.pickerViewObj numberOfRowsInComponent:0]);
}
这里是我的笔尖设置的图片,以防我在设置委托或数据源时出错。
通过上面的代码,当我触发(点击按钮)showAyat 方法时,它不会在运行时更改我的 uipickerview 的值,但我可以看到 numberOfRowsInComponent:0 已经使用 NSLog 更改为 3(之前为 2)。我不知道我的pickerview 没有更新显示有什么问题。 请有人在这里帮助我。
【问题讨论】:
标签: iphone objective-c