2012-09-07 164 views
0

我有一個uipickerview,我想從我的sqlite數據庫填充數據。從sqlite填充uipickerview

我已經通過appdelegate通過可變數組訪問數據,但是當我使用它作爲數據源時,它會在用作uipicker titleforrow部分時崩潰應用程序。

該數組的計數是正確的,它只是不會顯示任何數據!

這裏是我的頭文件:

@class OBMessage; 

@interface LocationViewController : UIViewController <UITableViewDelegate, UIPickerViewDelegate, UIPickerViewDataSource, UITableViewDataSource, UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, MFMailComposeViewControllerDelegate, MFMessageComposeViewControllerDelegate> { 
     UITableViewCell *cell1; 
     UITableViewCell *cell2; 
     UITableViewCell *cell3; 
     UITableViewCell *cell4; 
     UITableViewCell *cell5; 
     UITableViewCell *cell6; 
     UITableViewCell *cell7; 
    UITableViewCell *cell8; 
     IBOutlet UILabel *addressLabel; 
     MKPlacemark *placemark; 
     IBOutlet UILabel *lLat; 
     IBOutlet UILabel *lLong; 
     IBOutlet UILabel *lHAcc; 
     IBOutlet UILabel *lAlt; 
     IBOutlet UILabel *lVAcc; 
     IBOutlet UILabel *lDate; 
     UIPickerView *singlePicker; 
     NSArray *pickerData; 
     NSString *pickedInfo; 
     DadsTaxiV2AppDelegate *appDelegate; 
     NSMutableArray *myarray; 



} 
@property (nonatomic, retain) IBOutlet UITableViewCell *cell1; 
@property (nonatomic, retain) IBOutlet UITableViewCell *cell2; 
@property (nonatomic, retain) IBOutlet UITableViewCell *cell3; 
@property (nonatomic, retain) IBOutlet UITableViewCell *cell4; 
@property (nonatomic, retain) IBOutlet UITableViewCell *cell5; 
@property (nonatomic, retain) IBOutlet UITableViewCell *cell6; 
@property (nonatomic, retain) IBOutlet UITableViewCell *cell7; 
@property (nonatomic, retain) IBOutlet UITableViewCell *cell8; 
@property (nonatomic, retain) IBOutlet UILabel *addressLabel; 
@property (nonatomic, retain) IBOutlet UILabel *lLat; 
@property (nonatomic, retain) IBOutlet UILabel *lLong; 
@property (nonatomic, retain) IBOutlet UILabel *lHAcc; 
@property (nonatomic, retain) IBOutlet UILabel *lAlt; 
@property (nonatomic, retain) IBOutlet UILabel *lVAcc; 
@property (nonatomic, retain) IBOutlet UILabel *lDate; 
@property (nonatomic, retain) IBOutlet UIPickerView *singlePicker; 
@property (nonatomic, retain) NSArray *pickerData; 
@property (nonatomic, retain) NSString *pickedInfo; 
@property (nonatomic, retain) NSMutableArray *myarray; 


@property (nonatomic, retain) MKPlacemark *placemark; 

-(IBAction)popupCameraSheet:(id)sender; 
-(IBAction)popupMessageSheet:(id)sender; 
-(void)showPicker; 


@end 

這裏是我的m文件位:

- (void)viewDidLoad { 


     [super viewDidLoad]; 

    appDelegate = (DadsTaxiV2AppDelegate *)[[UIApplication sharedApplication] delegate]; 

    self.myarray = appDelegate.messageArray; 




} 


-(void)showPicker 
{ 
    UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:@"Select a Office Buddy Message to Include:" 
                 delegate:self 
              cancelButtonTitle:@"Select" destructiveButtonTitle:@"Cancel" 
              otherButtonTitles:nil]; 
    menu.tag = 4; 
    menu.actionSheetStyle = UIActionSheetStyleBlackTranslucent; 
    singlePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 185, 0, 0)]; 
    singlePicker.delegate=self; 
    singlePicker.showsSelectionIndicator = YES; 

    [singlePicker selectedRowInComponent:0]; 
    [singlePicker reloadComponent:0]; 

    [menu addSubview:singlePicker]; 
    [menu showInView:self.view]; 
    [menu setBounds:CGRectMake(0, 0, 320, 700)]; 

    [singlePicker release]; 
    [menu release]; 

    //NSInteger row = [singlePicker selectedRowInComponent:1]; 
    //NSString *selected = [pickerData objectAtIndex:row]; 

    //infoLabel.text = selected; 



} 

#pragma mark Picker Data Source Methods 
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 
{ 
    return 1; 
} 
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{ 
    //NSLog(@"[pickerData count]=%d",[pickerData count]); 
    //return [pickerData count]; 

    NSLog(@"[messagearray count]=%d",[self.myarray count]); 
    //return [self.pickerData count]; 
    return [self.myarray count]; 

} 

#pragma mark Picker Delegate Methods 
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 
    NSLog(@"[pickerData objectAtIndex:row]=%@",[self.myarray objectAtIndex:row]); 
    //return [self.pickerData objectAtIndex:row]; 
    return [self.myarray objectAtIndex:row]; 
} 

對不起忘了崩潰日誌:

didSelectViewController:<LocationViewController: 0x9b7de90> 
2012-09-07 20:43:07.142 OfficeBuddy[7300:c07] [messagearray count]=13 
2012-09-07 20:43:07.142 OfficeBuddy[7300:c07] [messagearray count]=13 
2012-09-07 20:43:07.142 OfficeBuddy[7300:c07] [pickerData objectAtIndex:row]=<OBMessage: 0x9a74ae0> 
2012-09-07 20:43:07.143 OfficeBuddy[7300:c07] -[OBMessage isEqualToString:]: unrecognized selector sent to instance 0x9a74ae0 
2012-09-07 20:43:07.144 OfficeBuddy[7300:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[OBMessage isEqualToString:]: unrecognized selector sent to instance 0x9a74ae0' 
*** First throw call stack: 
(0x1759022 0x1a5ecd6 0x175acbd 0x16bfed0 0x16bfcb2 0x8ed0ff 0xbcf17a 0x7a8bc0 0x847c54 0x8483ce 0x833cbd 0x83271a 0x83c220 0xbcf6bb 0x7a77ec 0x7a84f9 0x8ca1 0xcc71 0x83e5c5 0x83e7fa 0x47585d 0x172d936 0x172d3d7 0x1690790 0x168fd84 0x168fc9b 0x22847d8 0x228488a 0x7ad626 0x2f5d 0x2ed5) 
terminate called throwing an exception(lldb) 
+0

崩潰信息在哪裏? – J2theC

+2

@ J2theC和大家:http://emclstcd.tk – 2012-09-07 19:09:13

+0

如果你通過聲明一個在viewDidLoad中使用本地數組,這工作正常嗎?日誌對崩潰說什麼?是否有任何日誌語句在崩潰之前被註銷。 – Augie

回答

0

通過你的崩潰日誌,看起來你正在用你的數組填充的數據是OBMessage類的一個實例。當從數據源返回方法:

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 

確保您正在返回一個字符串。因此,如果您的數組由OBMessage的實例填充,請使用該類中的屬性來表示您想要在選取器上顯示的內容。例如,如果OBMessage有一個名爲messageStringRepresentation屬性,將返回:

return [[self.myarray objectAtIndex:row] messageStringRepresentation]; 
+0

輝煌的工作一種享受 - 謝謝:) – Jay

0

嘗試打印數組對象,並確認NSString對象。或者發佈代碼,如果您在代碼中某處使用了isEqualToString