如何在plist中获取特定项目以在tableView中显示

时间:2014-03-30 13:23:08

标签: ios objective-c uitableview

我有这种格式的plist:

enter image description here

我目前显示了plist的所有内容。我的主要问题是这个

如何仅根据类别显示?我将有3个类别,我想让用户能够在选择中看到该类别的项目。看看我的第一个项目,类别名称是'main'。如果用户选择主要我只需要显示主要类别项目。

我该如何做到这一点?

'

- (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...



'

含量

    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"];

这些是主要部分

1 个答案:

答案 0 :(得分:1)

我会创建一个新属性来保留已过滤的列表:

@property(nonatomic, strong) NSMutableArray *filteredContent;

然后更改tableView和segue方法,以便在过滤器处于活动状态时,它们将访问self.filteredContent数组而不是self.content。

最后,将过滤器更改为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

希望它能满足您的需求...