ios – 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:@"book"]);
        }
    }
}

提前致谢.

解决方法

更新: Expandable Rows JSON是您的项目,自动崩溃为所有行工作.

另外检查Expandable Rows,我打了这个项目来帮助诺瓦拉,并改进它.现在正在为所有部分工作,除了嵌套的行.

第一步

跟踪每个部分打开的行.当然这可以通过使用多个部分的打开的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:@"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_];
            }
        }
}

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...