UITableView控件上的不同行为

时间:2014-02-25 10:38:59

标签: ios iphone objective-c uitableview

我真的很感激这个控件:github link问题是现在我遇到的情况是,当我需要打开一行并且所有之前的那些应该关闭时,我只能有一个打开的行时间。

由于我对xcode不是很熟悉,我需要一些关于我应该从哪里开始的信息以及使代码按照我现在需要的方式工作的必要步骤。

我设法做了一些测试但不是一个可行的解决方案,这就是我现在所拥有的:

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

    NSInteger row = indexPath.row;

    if (indexPath.section == 0) {

        NSLog(@"indexPath1 = %i", selectedRow);

        NSDictionary *d = [self.firstForTable objectAtIndex:indexPath.row];

        if([d valueForKey:@"Objects"]) {
            NSArray *ar = [d valueForKey:@"Objects"];

            NSUInteger count = indexPath.row +1;
            NSMutableArray *arCells = [NSMutableArray array];

            for(NSDictionary *dInner in ar ) {
                [arCells addObject:[NSIndexPath indexPathForRow:count inSection:0]];
                [self.firstForTable insertObject:dInner atIndex:count++];
            }

            [tableView insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationLeft];
        }
        else {
            NSLog(@"Leave Element:::%@ %@|",[d valueForKey:@"name"],[d valueForKey:@"book"]);
        }

        if (selectedRow == row) {
            NSLog(@"selectedRow2 = %i",selectedRow);

            NSDictionary *d=[self.firstForTable objectAtIndex:selectedRow];

            if([d valueForKey:@"Objects"]) {
                NSArray *ar = [d valueForKey:@"Objects"];

                [self miniMizeFirstsRows:ar];
            }

            selectedRow = -1;
            return;
        }

        if (selectedRow >= 0) {
            NSLog(@"selectedRow3 = %i",selectedRow);

            NSDictionary *d=[self.firstForTable objectAtIndex:selectedRow];

            if([d valueForKey:@"Objects"]) {
                NSArray *ar = [d valueForKey:@"Objects"];

                [self miniMizeFirstsRows:ar];
            }

            selectedRow = row;
        }

        selectedRow = row;
        [tableView beginUpdates]; [tableView endUpdates];
    }


    if (indexPath.section == 1) {
        NSDictionary *d = [self.secondForTable objectAtIndex:indexPath.row];

        if([d valueForKey:@"Objects"]) {
            NSArray *ar = [d valueForKey:@"Objects"];
            BOOL isAlreadyInserted=NO;

            for (NSDictionary *dInner in ar) {
                NSInteger index = [self.secondForTable indexOfObjectIdenticalTo:dInner];

                isAlreadyInserted = (index > 0 && index != NSIntegerMax);
                if (isAlreadyInserted) break;
            }

            if (isAlreadyInserted) {
                [self miniMizeSecondsRows:ar];
            }
            else {
                NSUInteger count = indexPath.row+1;
                NSMutableArray *arCells = [NSMutableArray array];

                for (NSDictionary *dInner in ar ) {
                    [arCells addObject:[NSIndexPath indexPathForRow:count inSection:1]];
                    [self.secondForTable insertObject:dInner atIndex:count++];
                }

                [tableView insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationLeft];
            }
        }
        else {
            NSLog(@"Leave Element:::%@ %@|",[d valueForKey:@"name"],[d valueForKey:@"book"]);
        }
    }
}

先谢谢。

2 个答案:

答案 0 :(得分:6)

更新Expandable Rows JSON是您的项目,自动崩溃适用于所有行。

同时检查Expandable Rows,我分叉了这个项目以帮助Novara并改进它。现在适用于所有部分,嵌套行除外。

第一步

跟踪每个部分的已打开行。当然,这可以使用tabOpened的NSArray来完成多个部分。

@interface RootViewController : UITableViewController {
    NSInteger tabOpened_1;
    NSInteger tabOpened_2;
}

第二步

接下来初始化那些带有“-1”值的值,对于没有打开的选项卡。

- (void)viewDidLoad
{
    ........

    [self.firstForTable addObjectsFromArray:self.firstArray];
    tabOpened_1 = -1;
    [self.secondForTable addObjectsFromArray:self.secondArray];
    tabOpened_2 = -1;
}

第三步

更改didSelectRowAtIndexPath

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

        if (indexPath.section==0) {
            NSDictionary *d=[self.firstForTable objectAtIndex:indexPath.row];
            if([d valueForKey:@"produtos"]) {
                if (tabOpened_1 == -1) {
                    NSLog(@"No tab opened in first section");
                    tabOpened_1 = indexPath.row;
                } else if(tabOpened_1 != indexPath.row && tabOpened_1 > indexPath.row){
                    NSDictionary *d_ = [self.firstForTable objectAtIndex:tabOpened_1];
                    NSArray *ar_ = [d_ valueForKey:@"produtos"];
                    [self miniMizeFirstsRows:ar_];
                    tabOpened_1 = indexPath.row;
                }

                NSArray *ar=[d valueForKey:@"produtos"];
                BOOL isAlreadyInserted=NO;

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

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

                if (tabOpened_1 == -1) {
                    NSLog(@"No tab opened in first section");
                    tabOpened_1 = indexPath.row;
                } else if(tabOpened_1 != indexPath.row && tabOpened_1 < indexPath.row){
                    NSDictionary *d_ = [self.firstForTable objectAtIndex:tabOpened_1];
                    NSArray *ar_ = [d_ valueForKey:@"produtos"];
                    [self miniMizeFirstsRows:ar_];
                    tabOpened_1 = indexPath.row - [ar_ count];
                    tabOpened_1 = (tabOpened_1 < 0) ? indexPath.row : tabOpened_1;
                }
            } else {
               NSLog(@"Leave Element:::%@ %@|",[d valueForKey:@"nome"],[d valueForKey:@"numeroConta"]);
            }
            for (int i=0; i < [[self.rowsPerSection objectAtIndex:indexPath.section] integerValue]; i++) {
                NSDictionary *d_ = [self.secondForTable objectAtIndex:i];
                NSArray *ar_ = [d_ valueForKey:@"produtos"];
                [self miniMizeSecondsRows:ar_];
            }
        }

        if (indexPath.section==1) {
            if (tabOpened_2 == -1) {
                NSLog(@"No tab opened in second section");
                tabOpened_2 = indexPath.row;
            } else if(tabOpened_2 != indexPath.row && tabOpened_2 > indexPath.row){
                NSDictionary *d_ = [self.secondForTable objectAtIndex:tabOpened_2];
                NSArray *ar_ = [d_ valueForKey:@"produtos"];
                [self miniMizeSecondsRows:ar_];
                tabOpened_2 = indexPath.row;
            }

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

                BOOL isAlreadyInserted=NO;

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

                if(isAlreadyInserted) {
                    [self miniMizeSecondsRows:ar];
                } else {

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

                if (tabOpened_2 == -1) {
                    NSLog(@"No tab opened in second section");
                    tabOpened_2 = indexPath.row;
                } else if(tabOpened_2 != indexPath.row && tabOpened_2 < indexPath.row){
                    NSDictionary *d_ = [self.secondForTable objectAtIndex:tabOpened_2];
                    NSArray *ar_ = [d_ valueForKey:@"produtos"];
                    [self miniMizeSecondsRows:ar_];
                    tabOpened_2 = indexPath.row - [ar_ count];
                    tabOpened_2 = (tabOpened_2 < 0) ? indexPath.row : tabOpened_2;
                }
            } else {
                //NSLog(@"Leave Element:::%@ %@|",[d valueForKey:@"nome"],[d valueForKey:@"book"]);
            }

            for (int i=0; i < [[self.rowsPerSection objectAtIndex:indexPath.section] integerValue]; i++) {
                NSDictionary *d_ = [self.firstForTable objectAtIndex:i];
                NSArray *ar_ = [d_ valueForKey:@"produtos"];
                [self miniMizeFirstsRows:ar_];
            }
        }
}

答案 1 :(得分:5)

你真的有必要使用你提到的控制吗?如果没有,我可以建议你另一个控制。

它是开源的,可在github https://github.com/Augustyniak/RATreeView上找到。 RATreeView是由我编写的(从您编写的内容)它具有您需要的功能。它的公共API基于UITableView,因此使用起来非常简单。它具有相当广泛的文档,可以让您获得所有需要的信息。链接的github的存储库包含示例项目,该项目准确显示如何在您自己的项目中开始使用它。

如果您开始使用它并提出一些问题,请询问。