简体   繁体   中英

PFQueryTableViewController if statement

I am learning how to develop ios applications and i have an issue which i need some help.

I use the following code to retrieve data from my class which is on parse.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:  (NSInteger)section
{
return [recipes count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"RecipeCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}

Recipe *recipe = [recipes objectAtIndex:indexPath.row];

UIImageView *imageView = (UIImageView*) [cell viewWithTag:100];
imageView.image = [UIImage imageNamed:recipe.imageFile];
UILabel *nameLabel = (UILabel*) [cell viewWithTag:101];
nameLabel.text = recipe.name;
UILabel *prepTimeLabel = (UILabel*) [cell viewWithTag:102];
prepTimeLabel.text = recipe.prepTime;

return cell;

}

My table holds the prep time value which is a numeric value for the time needed to prepare the recipe. I want the user to be able to view recipes based on their preparation time. Lets say i have a button that the user can see the quickest meals to prepare. I have tried experimenting with if statements and while loops but i didnt have much success. I have tried stuff such as if (prepTime stringValue isEqualToString:@"30 mins"] and then do the rest but i didnt work.

any help is appreciated. As i said i am just learning so please try to newbielize your explanation.

thank you

Using PFQueryTableViewController , you can specify the Parse query to use to retrieve the data for your table view. In this case, you can filter recipes based on prepTime .

In your view controller, implement something like the following:

- (PFQuery *)queryForTable
{
  PFQuery *query = [PFQuery queryWithClassName:@"yourRecipeClassName"];
  [query whereKey:@"prepTime" lessThan:self.maxPrepTime];
  /*
    Customise as needed, e.g., could be:
    [query whereKey:@"prepTime" equalTo:@30];
  */
  return query;
}

You could add a property to your view controller to hold the currently selected prepTime (eg @property NSNumber *maxPrepTime ), then to change the selected filter, do something like:

self.maxPrepTime = @60;
[self.tableView reloadData];

You shouldn't need to do any additional work within your tableView:cellForRowAtIndexPath: method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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