我被困住了。我打电话给PFQuery和我NSlog我的“类别” 这一切在调试器区域都很好。
我遇到麻烦的地方是使用这些数据从Parse.com填充我的UIPickerView。
这是我想出的。
请注意,我已经为Picker提供了必要的方法,我只需要使用解析中的数据来填充它。
_pickerData是一个填充选择器的NSArray,我认为我可以等于填充对象,但这不起作用。
编辑:到目前为止我有什么......
- (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];
}
答案 0 :(得分:0)
在将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");
}
}];
答案 1 :(得分:0)
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");
}
}];
在加载数据后使用[categoryPicker reloadAllComponents];
更新pickerView。
修改强>
设置代表&资源 //连接数据
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];
}
答案 2 :(得分:0)
试试这个,如上所述,你需要在查询找到对象并加载数组后重新加载pickerView。另请注意,这是一个PFObjects数组,你必须使用标题的键值,你不能只插入一个PFObject来进行titleForRow:DataSource调用。
- (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"];
}