我有一个 uipickerview,我想用我的 sqlite 数据库中的数据填充它。
我已经通过 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)