Advertisement
Guest User

Untitled

a guest
Sep 16th, 2014
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. // Do any additional setup after loading the view from its nib.
  5.  
  6. NSDictionary *dic = [[NSDictionary alloc]initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"data" ofType:@"plist"]];
  7. self.items = [dic valueForKey:@"Items"];
  8. self.itemsInTable = [[NSMutableArray alloc]init];
  9. [self.itemsInTable addObjectsFromArray:self.items];
  10.  
  11. }
  12.  
  13. #pragma mark - Table Configuration
  14.  
  15. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  16. {
  17. return 1;
  18. }
  19.  
  20. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  21. {
  22. return [self.itemsInTable count];
  23. }
  24.  
  25. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  26. {
  27. static NSString *simpleTableIdentifier = @"SimpleTableItem";
  28.  
  29. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
  30.  
  31. if (cell == nil) {
  32. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
  33. }
  34.  
  35. cell.textLabel.text = [[self.itemsInTable objectAtIndex:indexPath.row]valueForKey:@"Name"];
  36. cell.textLabel.textAlignment = NSTextAlignmentRight;
  37.  
  38. return cell;
  39. }
  40.  
  41. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  42. {
  43. NSDictionary *dic=[self.itemsInTable objectAtIndex:indexPath.row];
  44. if([dic valueForKey:@"SubItems"])
  45. {
  46. NSArray *arr=[dic valueForKey:@"SubItems"];
  47. BOOL isTableExpanded=NO;
  48.  
  49. for(NSDictionary *subitems in arr )
  50. {
  51. NSInteger index=[self.itemsInTable indexOfObjectIdenticalTo:subitems];
  52. isTableExpanded=(index>0 && index!=NSIntegerMax);
  53.  
  54.  
  55.  
  56. if(isTableExpanded) break;
  57. }
  58.  
  59. if(isTableExpanded)
  60. {
  61. [self CollapseRows:arr];
  62. }
  63. else
  64. {
  65. NSUInteger count=indexPath.row+1;
  66. NSMutableArray *arrCells=[NSMutableArray array];
  67. for(NSDictionary *dInner in arr )
  68. {
  69. [arrCells addObject:[NSIndexPath indexPathForRow:count inSection:0]];
  70. [self.itemsInTable insertObject:dInner atIndex:count++];
  71.  
  72. }
  73. [self.menuTableView insertRowsAtIndexPaths:arrCells withRowAnimation:UITableViewRowAnimationRight];
  74. }
  75.  
  76.  
  77. }
  78.  
  79. }
  80.  
  81. -(void)CollapseRows:(NSArray*)ar
  82. {
  83. for(NSDictionary *dInner in ar )
  84. {
  85. NSUInteger indexToRemove=[self.itemsInTable indexOfObjectIdenticalTo:dInner];
  86. NSArray *arInner=[dInner valueForKey:@"SubItems"];
  87. if(arInner && [arInner count]>0)
  88. {
  89.  
  90. [self CollapseRows:arInner];
  91. }
  92.  
  93. if([self.itemsInTable indexOfObjectIdenticalTo:dInner]!=NSNotFound)
  94. {
  95. [self.itemsInTable removeObjectIdenticalTo:dInner];
  96. [self.menuTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:
  97. [NSIndexPath indexPathForRow:indexToRemove inSection:0]
  98. ]
  99. withRowAnimation:UITableViewRowAnimationRight];
  100. }
  101. }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement