【问题标题】:error while fetch data to uipickerview?将数据提取到 uipickerview 时出错?
【发布时间】:2013-09-27 15:25:09
【问题描述】:

我想从核心数据实体中获取记录,并希望针对该实体输入其他记录。

我有两个核心数据实体类别和产品

我也建立了他们的关系。

我已经完成了类别部分。

现在我想针对所选类别添加产品。

为此,我使用UIPICKERVIEW 来显示类别,然后将名称存储在产品中。

这里是 NSObject 子类 Product.h

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@class Category;

@interface Product : NSManagedObject

@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSNumber * is_active;
@property (nonatomic, retain) NSString * descript;
@property (nonatomic, retain) Category *category;

@end

和.m文件

#import "Product.h"
#import "Category.h"


@implementation Product

@dynamic name;
@dynamic is_active;
@dynamic descript;
@dynamic category;

@end

我正在尝试在哪里显示选择器。

IMSAddProductViewController.h

#import <UIKit/UIKit.h>

@interface IMSAddProductViewController : UIViewController < UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate>

@property (weak, nonatomic) IBOutlet UIPickerView *categoryPicker;
@property (weak, nonatomic) IBOutlet UITextField *txtEnterName;
@property (weak, nonatomic) IBOutlet UITextField *txtEnterDescription;
@property (weak, nonatomic) IBOutlet UISwitch *txtIsActive;

@property(nonatomic,retain)NSArray *arr;
@property (readonly, strong, nonatomic) NSMutableArray *categoryArray;

- (IBAction)btnSave:(id)sender;

@结束

我不知道在这段代码中应该编辑什么来显示选择器记录..

IMSAddProductViewController.m

#import "IMSAddProductViewController.h"
#import "IMSAppDelegate.h"
#import "Product.h"
#import "Category.h"

@interface IMSAddProductViewController ()
{

    NSManagedObjectContext *context;
}
@end

@implementation IMSAddProductViewController
@synthesize arr;
@synthesize txtIsActive;
@synthesize categoryArray;
@synthesize txtEnterName;
@synthesize txtEnterDescription;


   - (void)viewDidLoad
    {
        [super viewDidLoad];

        IMSAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
        context = [appDelegate managedObjectContext];

        NSEntityDescription *category = [NSEntityDescription entityForName:@"Category" inManagedObjectContext:context];
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Category"];
        request.resultType = NSDictionaryResultType;

        request.propertiesToFetch = [NSArray arrayWithObject:[[category propertiesByName] objectForKey:@"arr"]];


        NSError *error;
        NSMutableArray *results = [[context executeFetchRequest:request error:&error] mutableCopy];
        if (results == nil) {
            //error handle here
        }
        [self setArr:results];
          NSLog (@"name: %@",categoryArray);
    }

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

    - (NSInteger)pickerView:(UIPickerView *)pickerView
    numberOfRowsInComponent:(NSInteger)component
    {
       // return _countryNames.count;
        return arr.count;
    }

    - (NSString *)pickerView:(UIPickerView *)pickerView
                 titleForRow:(NSInteger)row
                forComponent:(NSInteger)component
    {
        //return _countryNames[row];
        return arr[row];
    }


    -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
          inComponent:(NSInteger)component
    {
        categoryArray = [self.arr objectAtIndex:row];
    }

当我查看它时,它会抛出异常

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:

'* -[__NSPlaceholderArray initWithObjects:count:]: 尝试插入 nil 对象 来自对象[0]'

【问题讨论】:

  • 你有没有在if (results == nil) {//error handle here}下断点看看是否正常?

标签: ios objective-c xcode core-data uipickerview


【解决方案1】:

根据official documentation.,需要将NSFetchRequestresultType设置为NSDictionaryResultType
request.propertiesToFetch 之后将其包含在viewDidLoad 中:

request.returnsDistinctResults = YES;   
request.resultType = NSDictionaryResultType;

另外,替换

[self setArr:results];

NSArray* results2 = [resultsvalueForKeyPath:@"arra"];
[self setArr:results2];

【讨论】:

  • @property (weak, nonatomic) IBOutlet UIPickerView *categoryPicker; Puneet 你看我没有在上面的任何地方使用它,因为在上面添加一些代码会显示我猜的结果
  • 首先你必须有获取的值。上面的代码将为您获取它。然后你必须将 UIPickerViewDelegate 设置为 self。然后您设置的代表将开始工作。使用调试来确保您正在获取对象和值并调用委托方法。
  • request.propertiesToFetch = [NSArray arrayWithObject:[[category propertiesByName] objectForKey:@"name"]]; Puneet 我在视图中编辑了这一行确实加载也编辑了这个 [self setArr:results]; NSLog(@"name: %@",arr);
  • 现在我得到了类别的名称,你能告诉我现在如何将它们传递给 uipickerview
  • 你是否在你的 nib 中将 UIPickerView 的委托属性设置为你的视图控制器?如果你还没有,你可以通过编程来完成。在 viewDidLoad 方法中写某处.. self.categoryPicker.delegate = self;然后将调用您已经设置的委托方法。我唯一不喜欢的是你引用你的 arr 对象的方式。在委托方法中将 arr 替换为 self.arr。
猜你喜欢
  • 2018-04-13
  • 1970-01-01
  • 2014-09-05
  • 1970-01-01
  • 2015-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-16
相关资源
最近更新 更多