ios – UITableView控件上的不同行为

前端之家收集整理的这篇文章主要介绍了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_];
            }
        }
}

猜你在找的iOS相关文章