2016-01-28 28 views
0

我試圖從解析中獲取數據。它有4-5個字段格式的字段,並將它們顯示在IOS故事板的下拉菜單中。獲取對象從解析到數組並轉換爲字符串

實際上有9列包括解析默認列。他們創建數據,ACL,對象ID等等。

我需要只從字符串格式解析那些字段,我想顯示它們作爲下拉菜單在UIViewController。對於下拉我是UITableViewController。 serviceview.h

@interface ServiceViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource, UITableViewDataSource,UITableViewDelegate> 

@property (strong, nonatomic)NSArray *selectionArray; 

@property (weak, nonatomic) IBOutlet UILabel *resultLabel; 

@property (weak, nonatomic) IBOutlet UIPickerView *servicepicker; 

@property (weak, nonatomic) IBOutlet UITableView *tableViewCars; 

@property (strong, nonatomic)NSArray *customerCars; 

- (IBAction)customerCarBtn:(id)sender; 

serviceview.m

@interface ServiceViewController() 

@end 

@implementation ServiceViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
    self.tableViewCars.delegate = self; 
    self.tableViewCars.dataSource = self; 
    _selectionArray = @[@"Car Service", @"Brake Pads", @"Car Battery", @"Alternator",@"Starter Motor",@"Timing Belt",@"Cooling System",@"Clutch Repair",@"Repair-Others"]; 
    NSString *uType = [[PFUser currentUser] objectForKey:@"email"]; 

    PFQuery *query = [PFQuery queryWithClassName:@"customerCars"]; 
    [query whereKey:@"cEmail" containsString:uType]; 
    NSLog(@"%@",uType); 
    [query findObjectsInBackgroundWithBlock:^(NSArray *customerCar, NSError *error) { 
     if (customerCar) { 
      NSLog(@"Successfully retrieved %lu scores.", (unsigned long)customerCar.count); 
      for (PFObject *objects in customerCar) { 
       NSLog(@"%@", objects.objectId); 
       _customerCars = @[objects]; 
       self.customerCars = [[NSArray alloc]initWithObjects:@[objects], nil]; 
       // NSLog(@"%@",[_customerCars.objectId]); 
      } 
      //NSLog(customerCars); 
     } 
    }]; 
} 

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


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 

    return [self.customerCars count]; 

} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 


    static NSString *simpleTableIdentifier = @"SimpleTableItem"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 



    if (cell == nil) { 

     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; 

    } 


    cell.textLabel.text = [self.customerCars objectAtIndex:indexPath.row] ; 

    //cell.textLabel.font = [UIFont systemFontOfSize:11.0]; 


    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 


    UITableViewCell *cell = [self.tableViewCars cellForRowAtIndexPath:indexPath]; 


    self.tableViewCars.hidden = YES; 

} 


- (IBAction)customerCarBtn:(id)sender { 

    if (self.tableViewCars.hidden == YES) { 
     self.tableViewCars.hidden = NO; 
    } 

    else 
     self.tableViewCars.hidden = YES; 


} 
+0

意味着,只想要,對具有字符串格式值的字段。 –

+0

你有沒有解決方案? –

+0

@HardikShekhat是的我有解決方案。使用NSDictionary來實現它。 –

回答

0

的對象存儲在您的customerCars陣列,是PFObject類型的,因爲這是解析的回報。

爲了從汽車的一個Parse屬性中獲取字符串值,請將其作爲字典鍵在對象上訪問。

例如,假設您想更新單元格文本標籤以顯示汽車的「顏色」屬性。你會使用類似的東西:

PFObject *car = [self.customerCars objectAtIndex:indexPath.row]; 
cell.textLabel.text = [NSString stringWithFormat: @"%@ - %@", car[@"make"], car[@"model"]]; 
+0

解析應該返回5行數據,我希望以NSString格式存儲每一條記錄,如何將其歸檔。 –

+0

返回的每行數據都是PFObject的形式,它不是一個String。要將其轉換爲NSString,請從對象中獲取值,然後將它們附加在一起。 Parse數據庫中每行數據的屬性(列)是什麼? –

+0

OBJECTID,化妝,模型,年, cEmail, createdAt, updatedAt, ACL,ACL雷戈以上是列在我的解析。我需要取得製造,模型,雷諾,一年。 –

0

首先,你必須檢查字段格式的值的字段解析。

然後,你必須使用PFQuery的selectKeys方法,並傳遞鍵名在數組來從解析唯一選擇的鍵是這樣的:

PFQuery *query = [PFQuery queryWithClassName:@"customerCars"]; 
[query whereKey:@"cEmail" containsString:uType]; 
[query selectKeys:@[@"column1",@"column2",@"column3"]]; 
相關問題