2013-08-19 76 views
0

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

这是展开和折叠

- (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]; 
     } 
    } 
} 
+0

您是否找到了解决方案?我也在寻找同样的解决方案... –

+0

Fahim bro你有解决方案吗? – user3182143

回答

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]; 
      } 
     } 
    } 

请原谅,如果任何支架遗漏。请通知我。

+0

我总共有三个级别,我可以如何管理它 –

+0

请Vijay_07过去所有代码扩展三个级别。与You.i同样的条件,我想要三个标签可扩展和崩溃和搜索第三级。 –