UIpickerview中未加载数据

时间:2016-11-04 16:26:18

标签: ios objective-c uipickerview

我正在尝试将数据从Web服务加载到UI选择器视图中。它显示为空白。以下是我的代码。

NSMutableArray * flooring;
NSDictionary *dictionary;
NSString *floorname;
NSString *floorid;

- (void)viewDidLoad {
    floorname = @"Name";
    floorid   = @"IntFloorId";
    NSString *strURL=[NSString stringWithFormat:@"http://123.63.83.11:8002/api/Floor/LoadFloorType"];
    NSData *jsonSource = [NSData dataWithContentsOfURL:
                          [NSURL URLWithString:strURL]];
    id jsonObjects = [NSJSONSerialization JSONObjectWithData:
                      jsonSource options:NSJSONReadingMutableContainers error:nil];
    for (NSDictionary *dataDict in jsonObjects) {
        NSString *Name = [dataDict objectForKey:@"Name"];
        NSString *floorid1 = [dataDict objectForKey:@"IntFloorId"];
        dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                     Name,floorname,floorid1,floorid, nil];
        [flooring addObject:dictionary];
    }


-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return flooring.count;
}
    - (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component  
{
    NSDictionary *tmpDict = [flooring objectAtIndex:row];
    NSString * f = [tmpDict objectForKey:@"Name"];return f;
}

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

flooring从未初始化,因此显示为空。

添加:

flooring = [NSMutableArray array];

viewDidLoad方法中。

还学会使用调试器。如果您调试了代码,很久以前就会发现这个。