dineshprasanna
dineshprasanna

Reputation: 1284

Dropdown in UITableview in ios using indentation levels

Here I want a Multilevel tableview. I was able to create it successfully but when one of the levels is selected, any previously expanded dropdown level should collapse. I used the sample code below.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"cellid"];
    if (cell==nil){
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellid"] autorelease];
    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    [cell.textLabel setText:[[arrtable objectAtIndex:indexPath.row] objectForKey:@"name"]];
    [cell setIndentationLevel:[[[arrtable objectAtIndex:indexPath.row] objectForKey:@"level"] intValue]];

    return cell;
}

When did select the row

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"arrtable :%@",arrtable);


    NSDictionary *d=[arrtable objectAtIndex:indexPath.row];
    NSLog(@"d%@",d);
    if([d valueForKey:@"objects"]) {

        NSArray *ar=[d valueForKey:@"objects"];

        BOOL isAlreadyInserted=NO;

        for(NSDictionary *dInner in ar ){
            NSInteger index=[arrtable 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]];
                [arrtable insertObject:dInner atIndex:count++];
            }
            [tableView insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationLeft];
        }
    }
}

- (void)miniMizeThisRows:(NSArray*)ar{
    for(NSDictionary *dInner in ar ) {
        NSUInteger indexToRemove=[arrtable indexOfObjectIdenticalTo:dInner];
        NSArray *arInner=[dInner valueForKey:@"objects"];
        if(arInner && [arInner count]>0){
            [self miniMizeThisRows:arInner];
        }
        if([arrtable indexOfObjectIdenticalTo:dInner]!=NSNotFound) {
            [arrtable removeObjectIdenticalTo:dInner];
            [testtable deleteRowsAtIndexPaths:[NSArray arrayWithObject:
                                                    [NSIndexPath indexPathForRow:indexToRemove inSection:0]
                                                    ]
                                  withRowAnimation:UITableViewRowAnimationRight];
        }
    }
}

Here I have a sample Image: Only I need to select one of the options

Upvotes: 2

Views: 4118

Answers (2)

Chigs79
Chigs79

Reputation: 162

https://www.dropbox.com/sh/1f7iqq0k98t780a/AAAhwHSCxAQPhaKBT0QDZATAa?dl=0

My Created own class for dropdown tableview i think it will be use full to you.

Upvotes: 2

umer sufyan
umer sufyan

Reputation: 1035

Well All i can do to provide You a link from there you can download that sample code which i hope so will help you in accomplishing this task TableView Link.

TreeView Tableview

tree Graph

Tree control

Coca Tree Example

Upvotes: 4

Related Questions