Itaws
Itaws

Reputation: 340

Get values from a plist file in iOS

I am creating a Plist file as shown below

enter image description here

I want to list all the Items where level is 1 and I can only use accessoryType = UITableViewCellAccessoryCheckmark if level is 1. How can I do it.

I am loading my plist file here:

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

}

My code for 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];
}

My code for didSelectRowAtIndexPath is :

-(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];
        }
    }
}

Please do reply. I am stuck here

Upvotes: 1

Views: 247

Answers (2)

Akhilrajtr
Akhilrajtr

Reputation: 5182

To check level = 1 to add accessoryType = UITableViewCellAccessoryCheckmark try

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

}

Upvotes: 1

John Rogers
John Rogers

Reputation: 2192

You can use the following code to get the root object, then go from there:

NSDictionary *rootDict = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"Root"];

Upvotes: 0

Related Questions