2015-04-30 2 views
1

Я создаю файл Plist, как показано нижеПолучить значения из файла Plist в прошивкой

enter image description here

Я хочу, чтобы перечислить все товары, где уровень равен 1, и я могу использовать только accessoryType = UITableViewCellAccessoryCheckmark, если уровень 1. Как я могу это сделать.

Я загрузке мой файл 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]; 
     } 
    } 
} 

Пожалуйста ответ. Я застрял здесь

+0

Если вы хотите перечислить все элементы, на которых уровень равен 1, и вы застряли в этой точке, вы должны предоставить нам код, который загружает plist из пакета и настройки на self.itemsInTable, а также 'tableview: cellFroRowAtIndexPath:', где вы должны set 'accessoryType = UITableViewCellAccessoryCheckmark '. – FormigaNinja

ответ

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 
    } 

} 
+0

Это проверка элементов, а не subItems. – Itaws

0

Вы можете использовать следующий код, чтобы получить корневой объект, а затем перейти оттуда:

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

Хорошо, но как я могу проверить, равно ли значение уровня 1. В настоящее время я использую условие , если ([[[dic valueForKey: @ "level"] objectAtIndex: 0] objectForKey: @ "1"]) который Дает ошибку – Itaws

Смежные вопросы