我已经创建了一个对象并将其保存到名为NewCar的后端,并且附加了4个字符串,其中包括年份,品牌,型号,马力我从解析数据中将数据拉到我的表中时遇到了麻烦这是我的一些代码。
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
PFQuery *query = [PFQuery queryWithClassName:@"NewCar"];
[query whereKey:@"year" equalTo:[[PFUser currentUser] objectId]];
[query orderByAscending:@"createdAt"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
} else {
self.cars = objects;
[self.tableView reloadData];
}
}];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.cars count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"garageCell";
garageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[garageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.usernameDisplay.text = [[PFUser currentUser] objectForKey:@"username"];
//I need to display the year, make and model below here.
cell.carYearLabel.text = [[PFObject objectWithClassName:@"NewCar"] objectForKey:@"year"];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 236;
}
我可以在自定义单元格中显示用户名,但无法弄清楚如何显示年份,品牌和型号值。任何帮助将不胜感激。
我一直在查看Parse中的文档,并且似乎无法弄明白,以防万一你想知道我是否在那里试过。
我还想在代码中添加我将对象添加到Parse中,这可能有助于解决这个问题。
- (IBAction)save:(id)sender {
NSString *year = self.carYear.text;
NSString *make = self.carMake.text;
NSString *model = self.carModel.text;
NSString *horsepower = self.carHorsepower.text;
if ([year length] == 0 || [make length] == 0 || [model length] == 0 || [horsepower length] == 0) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"You might have missed a field" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alertView show];
} else {
PFObject *newCar = [PFObject objectWithClassName:@"NewCar"];
newCar[@"year"] = year;
newCar[@"make"] = make;
newCar[@"model"] = model;
newCar[@"horsepower"] = horsepower;
[newCar saveInBackground];
[self.navigationController popToRootViewControllerAnimated:YES];
}
}
答案 0 :(得分:0)
好的,而不是使用
cell.carYearLabel.text = [[PFObject objectWithClassName:@"NewCar"] objectForKey:@"year"];
您可以使用以下
cell.carYearLabel.text = [self.cars objectForKey:@"NewCar"] valueForKey:@"year"];