【问题标题】:Dropdown in UITableview in ios using indentation levelsios中UITableview中的下拉菜单使用缩进级别
【发布时间】:2013-05-10 01:31:48
【问题描述】:

这里我想要一个多级表格视图。 I was able to create it successfully but when one of the levels is selected, any previously expanded dropdown level should collapse.我使用了下面的示例代码。

- (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;
}

什么时候选择了行

- (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];
        }
    }
}

这里有一个示例图片:

【问题讨论】:

标签: iphone ios objective-c uitableview


【解决方案1】:

我能做的就是为您提供一个链接,您可以从那里下载该示例代码,我希望它能帮助您完成这项任务TableView Link

TreeView Tableview

tree Graph

Tree control

Coca Tree Example

【讨论】:

  • 谢谢,但是当我单击子级别时,它会像树结构一样移动到另一个子级别。上面的代码只描述了一个级别,我如何才能移动到另外两个级别,你能帮我吗
  • 我已经为你编辑了答案,如果对你有帮助,请检查一下。
【解决方案2】:

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

我为下拉表格视图创建了自己的类,我认为它会对你完全有用。

【讨论】:

    猜你喜欢
    • 2013-12-03
    • 2012-07-19
    • 1970-01-01
    • 2011-11-29
    • 2012-01-14
    • 1970-01-01
    • 1970-01-01
    • 2018-09-20
    • 1970-01-01
    相关资源
    最近更新 更多