2012-03-03 46 views
1

我在UIPickerView中實現了下面顯示響應數據的代碼。它在我運行應用程序時崩潰。如何解決問題?pickerview的代碼如何在pickerview中顯示解析的url響應?

- (void)viewDidLoad { 
    [super viewDidLoad]; 


    self.view.frame=CGRectMake(0, 0, 320, 480); 
    materialarray = [[NSMutableArray alloc] init]; 



    [picbutton setTitle:[materialarray objectAtIndex:0] forState:UIControlStateNormal]; 
    [self testing]; 
     } 

-(IBAction)picbuttonaction:(id)sender{ 

    NSLog(@"unitpiker"); 
    [enterVal1 resignFirstResponder]; 
    [enterVal2 resignFirstResponder]; 


    str = picbutton.currentTitle; 
    UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:nil 
                 delegate:self 
              cancelButtonTitle:@"Done" 
             destructiveButtonTitle:nil 
              otherButtonTitles:nil]; 


    Picker = [[UIPickerView alloc] init]; 
    Picker.tag = 1; 
    Picker.showsSelectionIndicator = YES; 
    Picker.dataSource = self; 
    Picker.delegate = self; 

    [Picker selectRow:j inComponent:0 animated:YES]; 
    [menu addSubview:Picker]; 
    [menu showInView:self.view]; 
    [menu sendSubviewToBack:Picker];  
    [menu setBounds:CGRectMake(0,0,320, 300)]; 

    CGRect pickerRect = Picker.bounds; 
    Picker.bounds = pickerRect; 

    [Picker release]; 
    [menu release]; 
} 
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView; 
{ 
    if (pickerView.tag==1) { 
     return 1; 
    } 
    if(pickerView.tag==2){ 
     return 1; 
    } 
    return 0; 
} 
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{ 
    if (pickerView.tag==1){ 

     return [materialarray count]; 
    } 
    if(pickerView.tag==2){ 
     return [array_preferences count]; 
    } 
    return 0; 
} 
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component; 
{ 
    if (pickerView.tag==1) { 

     return [materialarray objectAtIndex:row]; 
    } 
    if(pickerView.tag==2){ 
     return [array_preferences objectAtIndex:row]; 
    } 
    return 0; 
} 
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component; 
{ 
    j=row; 

    [picbutton setTitle:[materialarray objectAtIndex:row] forState:UIControlStateNormal]; 
    }  

-(IBAction)Resetbuttonaction{ 

    [picbutton setTitle:[materialarray objectAtIndex:0] forState:UIControlStateNormal]; 
    j=0; 
} 


-(void)testing{ 
    NSString *jobSearchUrlString = [NSString stringWithFormat:@"http://infra2appsmobile.cloudapp.net/Infra2Apps.svc/Contacts"]; 
    //NSLog(@"url for new articles is = %@",jobSearchUrlString); 
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:jobSearchUrlString]]; 

    NSURLConnection *theconnection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
    if (theconnection) { 
     RoutData = [[NSMutableData alloc] init]; 
    } 

} 
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 

    [RoutData setLength: 0]; 
} 
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    [RoutData appendData:data]; 

    NSString *thexml = [[NSString alloc] initWithData:RoutData encoding:NSUTF8StringEncoding]; 

    NSDictionary *dictXML= [XMLReader dictionaryForXMLString:thexml error:nil]; 

    NSMutableArray *arrStation = [[dictXML objectForKey:@"feed"] objectForKey:@"entry"] ;//this would 



    for (int i=0; i<[arrStation count]; i++){ 
     str=[[[[[arrStation objectAtIndex:i] objectForKey:@"content"] objectForKey:@"m:properties"] objectForKey:@"d:ContactName"] objectForKey:@"text"]; 
     // NSlog(@"name is : %@",str); 

     [materialarray addObject:str]; 
     [Picker reloadAllComponents]; 
     NSLog(@"name is->>>>>>> : %@",materialarray);   
    } 

    /


} 

當我運行應用程序它顯示:

2012-03-03 12:09:55.665 AdditionNumbers[1698:f803] Could not load the "dropdown2.png" image referenced from a nib in the bundle with identifier "com.yourcompany.AdditionNumbers" 
2012-03-03 12:09:55.909 AdditionNumbers[1698:f803] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' 
*** First throw call stack: 
(0x14d1052 0x11bcd0a 0x14bddb8 0x2f0f 0x43064e 0x2abc 0x3689d6 0x3698a6 0x378743 0x3791f8 0x36caa9 0x1bc7fa9 0x14a51c5 0x140a022 0x140890a 0x1407db4 0x1407ccb 0x3692a7 0x36aa9b 0x2a2d 0x29a5) 
terminate called throwing an exceptionCurrent 

我得到響應象下面我必須表現出人與數據進入pickerview它是如何可能?

name is->>>>>>> : (
    "\n  Enrico Gaetani", 
    "\n  J\U00e9r\U00f4me Tredan" 
) 
2012-03-03 12:01:27.865 UIPickerView[1654:f803] name is->>>>>>> : (
    "\n  Enrico Gaetani", 
    "\n  J\U00e9r\U00f4me Tredan", 
    "\n  Mike Tsalidis" 
) 
2012-03-03 12:01:27.865 UIPickerView[1654:f803] name is->>>>>>> : (
    "\n  Enrico Gaetani", 
    "\n  J\U00e9r\U00f4me Tredan", 
    "\n  Mike Tsalidis", 
    "\n  Dana Sugarman" 
) 
2012-03-03 12:01:27.866 UIPickerView[1654:f803] name is->>>>>>> : (
    "\n  Enrico Gaetani", 
    "\n  J\U00e9r\U00f4me Tredan", 
    "\n  Mike Tsalidis", 
    "\n  Dana Sugarman", 
    "\n  Necip Ozyucel" 
) 
2012-03-03 12:01:27.867 UIPickerView[1654:f803] name is->>>>>>> : (
    "\n  Enrico Gaetani", 
    "\n  J\U00e9r\U00f4me Tredan", 
    "\n  Mike Tsalidis", 
    "\n  Dana Sugarman", 
    "\n  Necip Ozyucel", 
    "\n  Goksel Topbas" 
) 
2012-03-03 12:01:27.868 UIPickerView[1654:f803] name is->>>>>>> : (
    "\n  Enrico Gaetani", 
    "\n  J\U00e9r\U00f4me Tredan", 
    "\n  Mike Tsalidis", 
    "\n  Dana Sugarman", 
    "\n  Necip Ozyucel", 
    "\n  Goksel Topbas", 
    "\n  Nizar Abdul-Baki" 
) 
2012-03-03 12:01:27.951 UIPickerView[1654:f803] name is->>>>>>> : (
    "\n  Enrico Gaetani", 
    "\n  J\U00e9r\U00f4me Tredan", 
    "\n  Mike Tsalidis", 
    "\n  Dana Sugarman", 
    "\n  Necip Ozyucel", 
    "\n  Goksel Topbas", 
    "\n  Nizar Abdul-Baki", 
    "\n  Johannes Kanis" 
) 
+0

當它退出是什麼應用程序說呢? – EmilioPelaez 2012-03-03 06:17:03

+0

問題不是你的選擇器,發佈一些代碼你如何處理來自服務器的響應。錯誤表示「索引0超出空數組邊界」,顯然,響應數組爲零或零 – HelmiB 2012-03-03 08:19:25

+0

在加載數據之前,它看起來像在'viewDidLoad'中調用'[materialarray objectAtIndex:0]'。我會在那裏放一個臨時標題,然後在'connection:didReceiveData:'中更正它。 – 2012-03-21 18:58:27

回答

0

您是否在.h文件中設置了UIPickerView的代表?

它看起來像有在[materialarray]沒有數據,當你調用viewDidLoad