从iOS中的plist文件中获取值

时间:2015-04-30 04:05:30

标签: ios objective-c uitableview

我正在创建一个Plist文件,如下所示

enter image description here

我想列出级别为1的所有项目,如果级别为1,我只能使用accessoryType = UITableViewCellAccessoryCheckmark。我该怎么做。

我在这里加载我的plist文件:

- (void)viewDidLoad
{
    [super viewDidLoad];
    countId = 0;
    NSDictionary *dict=[[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Topic" ofType:@"plist"]];
    self.items=[dict valueForKey:@"Items"];
    self.itemsInTable=[[NSMutableArray alloc] init];
    [self.itemsInTable addObjectsFromArray:self.items];

    [self.menuTableView registerNib:[UINib nibWithNibName:NSStringFromClass([IndicatorTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([IndicatorTableViewCell class])];

    UIBarButtonItem *myButton = [[UIBarButtonItem alloc]
                                 initWithTitle:@"Done"
                                 style:UIBarButtonItemStylePlain
                                 target:self
                                 action:@selector(doneSelection:)];
    [self.navigationItem setRightBarButtonItem:myButton];

}

我的cellForRowAtIndexpath代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *Title= [[self.itemsInTable objectAtIndex:indexPath.row] valueForKey:@"Name"];
    return [self createCellWithTitle:Title image:[self.itemsInTable objectAtIndex:indexPath.row]  indexPath:indexPath];
}

我的didSelectRowAtIndexPath代码是:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic=[self.itemsInTable objectAtIndex:indexPath.row];
    if([dic valueForKey:@"SubItems"])
    {
        NSArray *arr=[dic valueForKey:@"SubItems"];

        BOOL isTableExpanded=NO;

        for(NSDictionary *subitems in arr )
        {
            NSInteger index=[self.itemsInTable indexOfObjectIdenticalTo:subitems];
            isTableExpanded=(index>0 && index!=NSIntegerMax);
            if(isTableExpanded) break;
        }

        if(isTableExpanded)
        {
            [self CollapseRows:arr];
        }
        else
        {
            NSUInteger count=indexPath.row+1;
            NSMutableArray *arrCells=[NSMutableArray array];
            for(NSDictionary *dInner in arr )
            {
                [arrCells addObject:[NSIndexPath indexPathForRow:count inSection:0]];
                [self.itemsInTable insertObject:dInner atIndex:count++];
            }
            [self.menuTableView insertRowsAtIndexPaths:arrCells withRowAnimation:UITableViewRowAnimationLeft];
        }
    }

    if([[[dic valueForKey:@"SubItems"]objectAtIndex:0]objectForKey:@"level"])
    {
//        NSArray *arr=[dic valueForKey:@"SubItems"];
//        if ([[arr objectAtIndex:0 ] intValue] == @"1")
        {
            UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

            if (cell.accessoryType == UITableViewCellAccessoryNone)
            {
                if(countId <5)
                {
                    cell.accessoryType = UITableViewCellAccessoryCheckmark;
                    countId = countId + 1;
                }
                else
                {
                    UIAlertView *dialog;

                    dialog =[[UIAlertView alloc] initWithTitle:@"Alert Message"
                                                       message:@"Select maximum 5 countries"
                                                      delegate:self
                                             cancelButtonTitle:@"Cancel"
                                             otherButtonTitles:@"OK",nil];
                    [dialog show];

                    //            NSLog(@"Greater then 5");
                }
            }

            else
            {
                if(countId>0)
                {
                    cell.accessoryType = UITableViewCellAccessoryNone;
                    countId--;
                }
                else
                {
                    //show alert
                    UIAlertView *dialog;

                    dialog =[[UIAlertView alloc] initWithTitle:@"Alert Message"
                                                       message:@"Select atleast 1 country"
                                                      delegate:self
                                             cancelButtonTitle:@"Cancel"
                                             otherButtonTitles:@"OK",nil];
                    [dialog show];

                    //            NSLog(@"must choose 1");
                }

            }
            //    countId = [self.tableView indexPathsForSelectedRows].count;

            [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
        }
    }
}

请回复。我被困在这里

2 个答案:

答案 0 :(得分:1)

要检查level = 1以添加accessoryType = UITableViewCellAccessoryCheckmark,请尝试

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *itemDictionary = [self.itemsInTable objectAtIndex:indexPath.row];
    NSArray *subItems = [itemDictionary valueForKey:@"SubItems"];
    NSDictionary *firstItem = subItems[0];
    if ([[firstItem objectForKey:@"level"] integerValue] == 1) {

        //Set appropriate accessory view here
    } else {

        //Check the cell accessory type and update this too
        //This is to avoid wrong accessory view on cell reuse
    }

}

答案 1 :(得分:0)

您可以使用以下代码获取根对象,然后从那里开始:

NSDictionary *rootDict = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"Root"];
相关问题