2013-08-19 77 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同樣的條件,我想要三個標籤可擴展和崩潰和搜索第三級。 –