0

我正在使用一个 git hub 教程中的 plist 值来扩展和折叠表格视图。在那个教程中,当我按下行时它正在扩展,当我按下同一行时,它再次折叠没有问题。但我想要的是,当我按下一行时,期望其他打开的行应该折叠,所以任何人都可以给我一些想法。

这是展开和折叠的代码

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

    NSDictionary *d=[self.arForTable objectAtIndex:indexPath.row];
    NSLog(@"%@",d);
    if([d valueForKey:@"Objects"]) {
        NSArray *ar=[d valueForKey:@"Objects"];
        NSLog(@"%@",ar);


        BOOL isAlreadyInserted=NO;

        for(NSDictionary *dInner in ar ){
            NSInteger index=[self.arForTable indexOfObjectIdenticalTo:dInner];
            NSLog(@"%ld",(long)index);


            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]];
                [self.arForTable insertObject:dInner atIndex:count++];
            }




            [tableView insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationLeft];
        }
    }
}

-(void)miniMizeThisRows:(NSArray*)ar{

    for(NSDictionary *dInner in ar ) {
        NSUInteger indexToRemove=[self.arForTable indexOfObjectIdenticalTo:dInner];

        NSArray *arInner=[dInner valueForKey:@"Objects"];
        if(arInner && [arInner count]>0){
            [self miniMizeThisRows:arInner];
        }

        if([self.arForTable indexOfObjectIdenticalTo:dInner]!=NSNotFound)
        {
            [self.arForTable removeObjectIdenticalTo:dInner];
            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:
                                                [NSIndexPath indexPathForRow:indexToRemove inSection:1]
                                                ]
                              withRowAnimation:UITableViewRowAnimationRight];
        }
    }
}
4

1 回答 1

0

这意味着您希望一次只扩展一个单元格。所以你应该保存最后一个展开行的索引路径,你应该为那个索引路径调用你的方法,同时你应该为你期望扩展的行插入单元格。

更新 在 .h 中创建成员变量

NSIndexPath *lastIndexPath;

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


            if(lastIndexPath.row == indexPath.row)
            {
          NSDictionary *d=[self.arForTable objectAtIndex:indexPath.row];
        if([d valueForKey:@"Objects"]) {
            NSArray *ar=[d valueForKey:@"Objects"];
              }
          [self miniMizeThisRows:ar];
            }

            else
            {
                 if(lastIndexPath.row)
               {
                    NSDictionary *d=[self.arForTable objectAtIndex:lastIndexPath.row];
                   if([d valueForKey:@"Objects"]) {
                   NSArray *ar=[d valueForKey:@"Objects"];
                   [self miniMizeThisRows:ar];
                   }
               }
                NSUInteger count=indexPath.row+1;
                NSMutableArray *arCells=[NSMutableArray array];
                for(NSDictionary *dInner in ar ) {
                    [arCells addObject:[NSIndexPath indexPathForRow:count inSection:0]];
                    [self.arForTable insertObject:dInner atIndex:count++];
                    lastIndexPath = indexPath;

                [tableView insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationLeft];
            }
        }
    }

如果遗漏了任何括号,请原谅。请通知我。

于 2013-08-19T11:26:22.957 回答