2016-11-04 74 views
-2

我想从Web服务加载数据到UI选取器视图。它显示为空白。以下是我的代码。数据不在UIpickerview加载

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; 
} 

任何帮助将不胜感激。

+0

包含的类是否符合UIPickerViewDelegate和UIPickerViewDataSource? –

回答

2

flooring从不初始化,所以它看起来是空的。

地址:

flooring = [NSMutableArray array]; 

viewDidLoad方法。

还学会使用您的调试器。如果你调试了你的代码,你很早以前就会发现它。

+0

不错。它的工作是我的错误。我忘记初始化阵列。还有更多的疑问吗?我可以在评论中提问吗? –

+0

如果您有与此直接相关的简单问题,您可以在此处发表评论。如果它不相关,那么在完成足够的研究之后,您应该发布一个新问题。 – rmaddy

+0

好的,我会问作为单独的问题。 –