2014-10-10 74 views
1

我被卡住了。我已經調用了PFQuery和我的NSlog我的「類別」 ,它在調試器區域都很好。如何使用Parse.com填充UIPickerView

我遇到麻煩的是使用這些數據從Parse.com填充我的UIPickerView。

這是我想出來的。

請注意,我已經爲拾取器已經有必要的方法,我只需要使用解析的數據來填充它。

_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

試試這個,正如上面所說,你需要重新加載pickerView在您的查詢找到對象並加載數組後。另請注意,這是一個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"]; 
} 
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"); 
    } 
}]; 
+0

它不工作時,uipickerview被稱爲「categoryPicker」但填充選擇器的NSArray的是「pickerData」 – user2975275 2014-10-10 06:25:11

+0

重裝categoryPicker和分配對象爲pickerData – abhishekkharwar 2014-10-10 06:39:52

+0

我怎麼會去這樣做呢? – user2975275 2014-10-10 06:46:03

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]; 
} 
+0

未能正常工作,uipickerview被稱爲'categoryPicker',但填充選擇器的nsarray是'pickerData' – user2975275 2014-10-10 06:25:47

+0

使用pickerData作爲pickerView的數據源 – 2014-10-10 06:33:03

+0

編輯例如 – 2014-10-10 06:35:32