1

I am creating accordion tableview cell using iOS storyboard. Here I have maintaining two tableview custom cell classes and cells on single storyboard within single tableview.

Now my problem is after selected the row based on plist storage It will expand (adding) additional rows at under the selected two. I need to use separate tableview cell for selected tableview cell (parent cell) and expandable cell (child cell). By my below code parent and child cell are showing and referring same cells(thats is parent only. So I cant differentiate UI for parent and child.

Need to modify below my logic.

-(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
        {
            // NEED TO ADD SECOND CELL HERE
            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.main_tableview insertRowsAtIndexPaths:arrCells withRowAnimation:UITableViewRowAnimationNone];
        }
    }
}
5
  • Okeh. And where is your cellForRowAtIndexPath function? Commented Dec 15, 2015 at 7:11
  • Check above I have updated give me one Idea!@Jorn Buitink Commented Dec 15, 2015 at 7:12
  • [CellIdentifier_one isEqualToString:@"Cell_One"] is always equal to YES? Commented Dec 15, 2015 at 7:18
  • [CellIdentifier_one isEqualToString:@"Cell_One"] is always true. Commented Dec 15, 2015 at 7:19
  • adding a screen shot of UI (what you are expecting) may help... Commented Dec 15, 2015 at 7:19

3 Answers 3

1

In following code I created multilevel of expandable UITableView using .plist file.

Read Data from .plist file

NSDictionary *dTmp = [[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"]];
self.arrayOriginal = [dTmp valueForKey:@"Objects"];

self.arForTable = [[NSMutableArray alloc] init];
[self.arForTable addObjectsFromArray:self.arrayOriginal];

Code for UITableView Delegates and Datasource.

   // Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.arForTable count];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
    }

    cell.textLabel.text=[[self.arForTable objectAtIndex:indexPath.row] valueForKey:@"name"];
    [cell setIndentationLevel:[[[self.arForTable objectAtIndex:indexPath.row] valueForKey:@"level"] intValue]];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    NSDictionary *d=[self.arForTable objectAtIndex:indexPath.row];
    if([d valueForKey:@"Objects"]) {
        NSArray *ar=[d valueForKey:@"Objects"];

        BOOL isAlreadyInserted=NO;

        for(NSDictionary *dInner in ar ){
            NSInteger index=[self.arForTable indexOfObjectIdenticalTo:dInner];
            isAlreadyInserted=(index>0 && index!=NSIntegerMax);
            if(isAlreadyInserted) break;
        }

        if(isAlreadyInserted) {
            [self miniMizeThisRows:ar];
        } else {
            NSUInteger count=indexPath.row+1;
            NSMutableArray *arCells=[NSMutableArray array];
            for(NSDictionary *dInner in ar ) {
                [arCells addObject:[NSIndexPath indexPathForRow:count inSection:0]];
                [self.arForTable insertObject:dInner atIndex:count++];
            }
            [tableView insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationLeft];
        }
    }
}

-(void)miniMizeThisRows:(NSArray*)ar{

    for(NSDictionary *dInner in ar ) {
        NSUInteger indexToRemove=[self.arForTable indexOfObjectIdenticalTo:dInner];
        NSArray *arInner=[dInner valueForKey:@"Objects"];
        if(arInner && [arInner count]>0){
            [self miniMizeThisRows:arInner];
        }

        if([self.arForTable indexOfObjectIdenticalTo:dInner]!=NSNotFound) {
            [self.arForTable removeObjectIdenticalTo:dInner];
            [self.tblForCollapse deleteRowsAtIndexPaths:[NSArray arrayWithObject:
                                                    [NSIndexPath indexPathForRow:indexToRemove inSection:0]
                                                    ]
                                  withRowAnimation:UITableViewRowAnimationRight];
        }
    }
}

Download the sample code from here.

Output

enter image description here

May this help to fulfil your requirement.

Sign up to request clarification or add additional context in comments.

8 Comments

Give me two min I am checking!@Nimit Parekh
Its really great I have one doubt from your source code. There sections you have used but I dont want to section how can I remove? only two process parent cell if parent cell have a subitems need to expand and show child cell. Both cells UI need to differentiate.plz tel me@Nimit Parekh
@Apple_Ajay Man, I am not using header that's why I am passing numberOfSectionsInTableView returns 1; I use directly rows if you don't want to child then just go to the .plist file and add one object and no child then it's self not expanding the parent controller.
How can I seperate two UI's thats my problem. Could you please check my above image post @Nimit Parekh
Parent and child UI not showing different both are same. Hocan I seperate that logics I dont know that is my plm also. Your code and mine both same the thing need to make different UI for parent and child.@Nimit Parekh
|
1

You would need to design two different table cell views, and instantiate either one in cellForRowAtIndexPath, depending on if it's a sub-item or a not.

Pretty much

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    id item = self.itemsInTable[indexPath.row];

    UITableViewCell *cell;

    if (item[@"isSubItem"]) {

        cell = [tableView dequeueReusableCellWithIdentifier:@"subItemCell"];

    } else {

        cell = [tableView dequeueReusableCellWithIdentifier:@"normalCell"];
    }

    // ...

    return cell;
}

1 Comment

Give me two min I am checking!
0

DSNestedAccordion does nested tableViews very well. Have a look here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.