在ios中选择其他行时展开/折叠单元格折叠行

时间:2013-08-19 10:53:23

标签: ios objective-c uitableview expand collapse

我正在使用来自一个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];
        }
    }
}

1 个答案:

答案 0 :(得分: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];
            }
        }
    }

如果错过了任何支架,请原谅。请通知我。