简体   繁体   English

UITableView控件上的不同行为

[英]Different behavior on UITableView control

There Is this control that I really appreciate: github link the problem is that now I have a situation where when I need to open one row and all the previous ones should close, I can only have one open row at the time. 有这个控件,我真的很感激: github链接问题是,现在我有一种情况,当我需要打开一行而所有以前的那些应该关闭时,我当时只能有一个打开的行。

Since I'm not very experienced with xcode, I need some info on where should I start and the necessary steps to make the code work the way I need right now. 由于我对xcode不是很熟悉,我需要一些关于我应该从哪里开始的信息以及使代码按照我现在需要的方式工作的必要步骤。

I managed to make some tests but not a viable solution, here is what I have right now: 我设法做了一些测试,但不是一个可行的解决方案,这就是我现在所拥有的:

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

Thanks in Advance. 提前致谢。

UPDATED : Expandable Rows JSON is your project, with auto-collapse working for all rows. 更新可扩展行JSON是您的项目,自动崩溃适用于所有行。

Also check Expandable Rows , i forked this project to help Novara and improve it. 同时检查Expandable Rows ,我分叉这个项目来帮助Novara并改进它。 Now working for all sections, except nested rows. 现在适用于所有部分,嵌套行除外。

FIRST STEP 第一步

Track opened row for each section. 跟踪每个部分的打开行。 Of course this can be done using NSArray of tabOpened for multiple sections. 当然,这可以使用tabOpened的NSArray来完成多个部分。

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

SECOND STEP 第二步

Next initialize those with "-1" value, for none opened tab. 接下来初始化那些带有“-1”值的值,对于没有打开的选项卡。

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

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

THIRD STEP 第三步

Change the didSelectRowAtIndexPath 更改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_];
            }
        }
}

Is this really necessary for you to use mentioned by you control? 你真的有必要使用你提到的控制吗? If not I can propose you another control. 如果没有,我可以建议你另一个控制。

It is open source and available on github https://github.com/Augustyniak/RATreeView . 它是开源的,可以在github上找到https://github.com/Augustyniak/RATreeView RATreeView was written by me and (from what you wrote) it has functionality you need. RATreeView是由我编写的(从您编写的内容)它具有您需要的功能。 Its public API is based on UITableView's one so it is really simple to use. 它的公共API基于UITableView,因此使用起来非常简单。 It has pretty expanded documentation which will allow you to get all needed information. 它具有相当广泛的文档,可以让您获得所有需要的信息。 Linked github's repository contains sample project which shows exactly how to start using it in your own project. 链接的github的存储库包含示例项目,该项目准确显示如何在您自己的项目中开始使用它。

In case you start to use it and have some questions just ask. 如果你开始使用它并有一些问题,请问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM