Reputation: 3
I've got a plist in this format:
I currently show all the content of plist.My main question is this
How can I show only based on the category? I will have 3 categories and I want to make the user able to see just that category's item in selection. Look at my first item, the category name is 'main'. If user select main I need to show only main categories items.
How can I accomplish this?
'
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
if( [[UIDevice currentDevice] userInterfaceIdiom]== UIUserInterfaceIdiomPhone) {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
}
cell.textLabel.text=[[self.content objectAtIndex:indexPath.row] valueForKey:@"recipeName"];
// NSInteger rating;
//NSString *reason;
// rating = [[self.content objectAtIndex:indexPath.row] valueForKey:@"rating"];
return cell;
// Configure the cell...
'
content
synthesize tableView,content=_content;
-(NSArray*) content {
if(!_content){
NSString *path = [[NSBundle mainBundle] pathForResource:@"recipes" ofType:@"plist" ];
_content = [[NSArray alloc] initWithContentsOfFile:path];
}
return _content;
}
segue part
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"showDetail"]) {
// Get destination view
detailViewController *destViewController = [segue destinationViewController];
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSInteger tagIndex = [(UIButton *)sender tag];
destViewController.recipeName =[[self.content objectAtIndex:indexPath.row] valueForKey:@"recipeName"];
destViewController.recipeDetail =[[self.content objectAtIndex:indexPath.row] valueForKey:@"recipeDetail"];
destViewController.recipeIngredients =[[self.content objectAtIndex:indexPath.row] valueForKey:@"recipeIngredients"];
these are the main parts
Upvotes: 0
Views: 167
Reputation: 2550
I would create a new property to keep the filtered list:
@property(nonatomic, strong) NSMutableArray *filteredContent;
Then change the tableView and segue methods so that if there is a filter active, they would access the self.filteredContent array instead of self.content.
Finally, when changing the filter to main:
NSMutableArray *array = [NSMutableArray array];
for (NSDictionary *dictionary in self.content)
if ([[dictionary objectForKey:@"category"] isEqualToString:@"main"])
[array addObject:dictionary];
self.filteredContent = array; //filtered list
[self.tableView reloadData]; //update the table view
Hope it works for what you need...
Upvotes: 1