简体   繁体   English

如何使用Parse.com填充UIPickerView

[英]How to populate UIPickerView with Parse.com

I'm stuck. 我被卡住了。 I have called the PFQuery and I NSlog my "Categories" and it all comes fine in the debugger area. 我已经将PFQuery称为“我的类别”,并且在调试器区域中一切正常。

Where I'm having trouble is using this data to populate my UIPickerView from Parse.com. 我遇到麻烦的地方是使用这些数据从Parse.com填充我的UIPickerView。

Here is what I came up with. 这是我想出的。

Note that I have the necessary methods already for the Picker, I just need to use the data from parse to populate it. 请注意,对于选择器我已经有了必要的方法,我只需要使用解析中的数据来填充它。

_pickerData is a NSArray that populates the picker and I thought I could equal to objects to populate but this didn't work. _pickerData是一个填充选择器的NSArray,我认为我可以等同于要填充的对象,但这没有用。

EDIT: What I have so far... 编辑:我到目前为止有...

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


    PFQuery *query = [PFQuery queryWithClassName:@"Categories"];
    [query whereKeyExists:@"Category"];
    [query orderByDescending:@"createdAt"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            _pickerData = objects;

            NSLog(@"%@",objects);
        }
        else {
            NSLog(@"error");
        }
    }];


    self.categoryPicker.dataSource = self;
    self.categoryPicker.delegate = self;


}


// The number of columns of data
- (int)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// The number of rows of data
- (int)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return _pickerData.count;
}

// The data to return for the row and component (column) that's being passed in
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return _pickerData[row];
}

Reload your picker view after assign objects to _pickerData objects分配给_pickerData后,重新加载选择器视图

PFQuery *query = [PFQuery queryWithClassName:@"Categories"];
[query whereKeyExists:@"Category"];
[query orderByDescending:@"createdAt"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        _pickerData = objects;

      [_categoryPicker reloadData];

      NSLog(@"%@",objects);
    }
    else {
        NSLog(@"error");
    }
}];
PFQuery *query = [PFQuery queryWithClassName:@"Categories"];
    [query whereKeyExists:@"Category"];
    [query orderByDescending:@"createdAt"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            _pickerData = objects;

             // update pickerView
             [categoryPicker reloadAllComponents];

            NSLog(@"%@",objects);
        }
        else {
            NSLog(@"error");
        }
    }];

Use [categoryPicker reloadAllComponents]; 使用[categoryPicker reloadAllComponents]; to update the pickerView after the data is loaded. 在加载数据后更新pickerView。

Edit: 编辑:

Set delegate & source // Connect data 设置委托人和来源//连接数据

self.categoryPicker.dataSource = self;
self.categoryPicker.delegate = self;

Add methods: 添加方法:

// The number of columns of data
- (int)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// The number of rows of data
- (int)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return _pickerData.count;
}

// The data to return for the row and component (column) that's being passed in
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return _pickerData[row];
}

Try this, as said above you need to reload your pickerView after your query finds the objects and loads the array. 尝试执行此操作,如上所述,您需要在查询找到对象并加载数组后重新加载pickerView。 Also note this is an array of PFObjects, and you must use a key-value for the title, you can't just plug in a PFObject for the titleForRow: DataSource call. 还要注意,这是一个PFObjects数组,您必须为标题使用键值,不能仅仅为titleForRow:DataSource调用插入PFObject。

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

    self.categoryPicker.dataSource = self;
    self.categoryPicker.delegate = self;

    PFQuery *query = [PFQuery queryWithClassName:@"Categories"];
    [query whereKeyExists:@"Category"];
    [query orderByDescending:@"createdAt"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            _pickerData = objects;
            [self.categoryPicker reloadAllComponents];
        }
        else {
            NSLog(@"error");
        }
    }];
}


// The number of columns of data
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

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

// The data to return for the row and component (column) that's being passed in
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    PFObject *object = _pickerData[row];
    //Assuming "Category" is a string here for your title
    return object[@"Category"];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM