【问题标题】:UIPickerView reloadAllComponents not workingUIPickerView reloadAllComponents 不起作用
【发布时间】: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


    【解决方案1】:

    问题:

    您正在创建两个UIPickerView

    • Xib 中的一个(连接为 IBOutlet)。
    • 代码中的第二个(在 viewDidLoad: 方法中)。

    两者都从您的delegate 获取数据值。

    由于在 Xib 中创建的选取器在您在代码中再初始化第二个 UIPickerView 时失去与 pickerViewObj 变量的连接。所以当时出现的UIPickerView 并没有改变它的值。

    解决方案:

    从代码中删除创建UIPickerView的代码。

    【讨论】:

    • woo u tottaly rite..,tx 真的节省了我的时间
    猜你喜欢
    • 2012-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多