【问题标题】:populate uipicker view with results from core data DB using an NSArray使用 NSArray 使用来自核心数据 DB 的结果填充 uipicker 视图
【发布时间】:2013-06-24 06:06:42
【问题描述】:

我正在尝试使用 NSFetchRequest 的结果填充 UIPickerView。然后将 NSFetchRequest 的结果存储在 NSArray 中。我正在使用 Core Data 与 SQLite DB 进行交互。我有一个简单的类文件,其中包含一个 UIPickerView,它与我的项目中的情节提要场景相关联。

类的头文件如下所示,

ViewControllerUsers.h

#import <UIKit/UIKit.h>
#import "AppDelegate.h"

@interface ViewControllerUsers : UIViewController <NSFetchedResultsControllerDelegate, UIPickerViewDelegate, UIPickerViewDataSource>
{

NSArray *dictionaries;
}
@property (nonatomic, strong) NSFetchedResultsController *fetchedResultsController;
// Core Data
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;

@property (nonatomic, strong) NSArray *users;

@property (strong, nonatomic) IBOutlet UIPickerView *uiPickerViewUsers;

@property (weak, nonatomic) IBOutlet UIBarButtonItem *btnDone;

@property (weak, nonatomic) IBOutlet UIButton *btnChangePin;

// added for testing purposes
@property (nonatomic, strong) NSArray *usernames;

- (IBAction)dismissScene:(id)sender;

- (IBAction)changePin:(id)sender;

@end

实现文件如下所示,

ViewControllerUsers.m

#import "ViewControllerUsers.h"

@interface ViewControllerUsers ()

@end

@implementation ViewControllerUsers

// Core Data
@synthesize managedObjectContext = _managedObjectContext;

@synthesize uiPickerViewUsers = _uiPickerViewUsers;

@synthesize usernames = _usernames;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    // Core Data
    if (_managedObjectContext == nil)
    {
        _managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
        NSLog(@"After _managedObjectContext: %@",  _managedObjectContext);
    }

    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Account"];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Account" inManagedObjectContext:_managedObjectContext];
    request.resultType = NSDictionaryResultType;
    request.propertiesToFetch = [NSArray arrayWithObject:[[entity propertiesByName] objectForKey:@"username"]];
    request.returnsDistinctResults = YES;

    _usernames = [_managedObjectContext executeFetchRequest:request error:nil];

    NSLog (@"names: %@",_usernames);
}


-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    //One column
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    //set number of rows
    return _usernames.count;
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    //set item per row
    return [_usernames objectAtIndex:row];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload {
    [self setBtnDone:nil];
    [self setUiPickerViewUsers:nil];
    [self setBtnChangePin:nil];
    [super viewDidUnload];
}
- (IBAction)dismissScene:(id)sender {

    [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)changePin:(id)sender {
}
@end

当前代码导致应用程序崩溃,但 NSLog 显示了 NSArray 中 NSFetchRequest 的结果。如果我不得不猜测的话,我目前认为我没有正确格式化 NSArray 中 NSFetchRequest 的结果。

崩溃日志如下所示,

2013-06-26 16:49:24.219 KegCop[41233:c07] 名称:( { 用户名=废话; }, { 用户名 = 克里斯; }, { 用户名 = 根; } ) 2013-06-26 16:49:24.223 KegCop[41233:c07]-[NSKnownKeysDictionary1 isEqualToString:]:无法识别的选择器发送到实例 0xe54d9a0 2013-06-26 16:49:24.223 KegCop[41233:c07] 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[NSKnownKeysDictionary1 isEqualToString:]:发送了无法识别的选择器到实例 0xe54d9a0' 首先抛出调用堆栈:

【问题讨论】:

    标签: ios objective-c core-data uipickerview


    【解决方案1】:

    您的 usernames 数组是字典数组,而不是字符串数组。

    像这样更新您的 titleForRow: 方法:

    -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
    {
        //set item per row
        return _usernames[row][@"username"];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-03
      • 2011-04-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-29
      相关资源
      最近更新 更多