Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view from its nib.
- NSDictionary *dic = [[NSDictionary alloc]initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"data" ofType:@"plist"]];
- self.items = [dic valueForKey:@"Items"];
- self.itemsInTable = [[NSMutableArray alloc]init];
- [self.itemsInTable addObjectsFromArray:self.items];
- }
- #pragma mark - Table Configuration
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- return 1;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- return [self.itemsInTable count];
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString *simpleTableIdentifier = @"SimpleTableItem";
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
- if (cell == nil) {
- cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
- }
- cell.textLabel.text = [[self.itemsInTable objectAtIndex:indexPath.row]valueForKey:@"Name"];
- cell.textLabel.textAlignment = NSTextAlignmentRight;
- return cell;
- }
- -(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:UITableViewRowAnimationRight];
- }
- }
- }
- -(void)CollapseRows:(NSArray*)ar
- {
- for(NSDictionary *dInner in ar )
- {
- NSUInteger indexToRemove=[self.itemsInTable indexOfObjectIdenticalTo:dInner];
- NSArray *arInner=[dInner valueForKey:@"SubItems"];
- if(arInner && [arInner count]>0)
- {
- [self CollapseRows:arInner];
- }
- if([self.itemsInTable indexOfObjectIdenticalTo:dInner]!=NSNotFound)
- {
- [self.itemsInTable removeObjectIdenticalTo:dInner];
- [self.menuTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:
- [NSIndexPath indexPathForRow:indexToRemove inSection:0]
- ]
- withRowAnimation:UITableViewRowAnimationRight];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement