2013-02-22 61 views
0

我正在將一個位置的源解析爲名爲items的NSMutableArray。我填充一個UIPickerView如下:從Feed中填充UIPickerView

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

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component { 
return [items count]; 
} 

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row  forComponent:(NSInteger)component { 
return [items objectAtIndex:row]; 
} 

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row 
    inComponent:(NSInteger)component 
{ 
id item = [items objectAtIndex:row]; 
NSString *occasion = [item objectForKey:@"name"]; 
location.text = occasion; 
} 

當我這樣做沒有任何值出現在UIPickerView,有人可以幫我這個好嗎?一切連接正確

回答

2

嘗試這樣

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

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent: (NSInteger)component { 
return [items count]; 
} 

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row  forComponent:(NSInteger)component { 
id item = [items objectAtIndex:row]; 
NSString *occasion = [item objectForKey:@"name"]; 
return occasion; 
} 

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row 
    inComponent:(NSInteger)component 
{ 
id item = [items objectAtIndex:row]; 
NSString *occasion = [item objectForKey:@"name"]; 
location.text = occasion; 
} 

這將顯示在選擇器串

pickerview還設置委託

我想你是通過整個dictionaty而不是字符串可能

+0

通過這樣做,我得到以下錯誤***終止應用程序,由於未捕獲的異常'NSInternalInconsistencyException',原因:'在無效索引路徑( 2個索引[0,0])'' – 2013-02-22 11:43:21

+0

您要傳遞的請求字典我想這樣嘗試通過NSString對象 – 2013-02-22 11:46:35

+0

你可以給我一些方向做這個請,我真的很感激它 – 2013-02-22 11:52:01

1

做這些事情:

#1確保你正確設置委託。例如。 :

self.pickerView.delegate = self; 

#2檢查您的場合字符串是否不爲零也不爲空。

NSLog(@"%@", occasion); 
+0

委託設置正確和數組不爲空數組:( 曼徹斯特,哈德斯菲爾德 , 埃蘭, 約維爾, 倫敦 ) – 2013-02-22 10:57:02

+0

您的數組不爲空,但您檢索數據' [item objectForKey:@「name」]'方法。檢查返回值是否爲空。 – Adam 2013-02-22 10:59:40

0

我猜項目數組可以是字典數組。所以,你應該在NSDictionary中得到NSString。

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row  forComponent:(NSInteger)component { 
return [[items objectForKey:@"name"] objectAtIndex:row]; 
}