populate uipickerview from sqlite

I have a uipickerview which i want to populate with data from my sqlite database.

I have got access to the data via a mutable array via the appdelegate but when i use that as the data source it is crashing the app when used as the uipicker titleforrow section.

The count from the array is correct it just will not display any data!

Here is my header file:

@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

Here is bits of my m file:

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

Sorry forgot the crash log:

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) 

Answers


By your crash log, it seems that the data you are populating your array with is an instance of the OBMessage class. When returning from the datasource method:

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

Make sure you are returning a string. So, if your array is populated by instances of the OBMessage, use a property from that class that represents what you want to present on your picker. For example, if OBMessage has a property called messageStringRepresentation, you would return:

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

Try to print the array objects and confirm NSString objects. or post a code if you have used isEqualToString somewhere in your code.


Need Your Help

How to convert only strings hash values that are numbers to integers

ruby string integer data-type-conversion

I have rows of hashes imported from several different XML database dumps that look like this (but with varying keys):

WCF JSON Return Service should return Single Escape Sequence

c# string wcf utf-8

What i am doing for changing html codes to JavaScript escapes is :

Can Mono create/run reports in rdlc?

c# .net mono rdlc

I've never used mono and was curious if mono can create/run rdlc reports? What I'm looking at implenenting is a mono asp.net mvc app to use a rdlc to create a report and export to pdf. Is this poss...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.