2016-05-11 50 views
1

嘿,我是新手在ios開發。我想展開和摺疊的tableview,所以我做它的代碼是:展開和摺疊桌面單元格

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

if (indexPath.section != 0) 
{ 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    NSDictionary *d=[self.arForTable objectAtIndex:indexPath.row]; 
    if([d valueForKey:@"Objects"]) { 
     NSArray *ar=[d valueForKey:@"Objects"]; 

     BOOL isAlreadyInserted=NO; 

     for(NSDictionary *dInner in ar){ 
      NSInteger index=[self.arForTable indexOfObjectIdenticalTo:dInner]; 
      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:1]]; 
       [self.arForTable insertObject:dInner atIndex:count++]; 
      } 
      [tableView insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationTop]; 
     } 
    } 
} 

和我的最小化等作爲

-(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.tblLeft deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexToRemove inSection:1]]withRowAnimation:UITableViewRowAnimationBottom]; 
    } 
} 
} 

從這個方法和編碼我實現了Expand and Collapse tableViewCell,該工程排方法。由於它在單擊時展開tableViewCell並在第二次單擊Expandable單元時將其摺疊。

但我想這樣工作:點擊任何可展開的單元格,其他可展開的單元格將自動崩潰。

任何人都可以幫助我嗎?

在此先感謝。

回答

0

當調用didSelectRowAtIndexPath:之後,可以將最後一個可展開的單元格保存爲摺疊狀態。

@interface後放:

@property (nonatomic, strong) NSIndexPath *lastIndexPath; 

現在你可以在你的didSelectRowAtIndexPath喜歡使用它:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    // Collapse the last cell expanded 
    if (indexPath.section != 0) { 
     if(self.lastIndexPath) { 
      UITableViewCell *cell = [tableView cellForRowAtIndexPath:self.lastIndexPath]; 
      NSDictionary *d=[self.arForTable objectAtIndex:self.lastIndexPath.row]; 
      if([d valueForKey:@"Objects"]) { 
       NSArray *ar=[d valueForKey:@"Objects"];    
      } [self miniMizeThisRows:ar]; 
     } 
    } 

    // Save the current indexPath as the last one expanded 
    self.lastIndexPath = indexPath; 

    // This is your current code - I didn't change it 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    NSDictionary *d=[self.arForTable objectAtIndex:indexPath.row]; 
    if([d valueForKey:@"Objects"]) { 
     NSArray *ar=[d valueForKey:@"Objects"]; 

     BOOL isAlreadyInserted=NO; 

     for(NSDictionary *dInner in ar){ 
      NSInteger index=[self.arForTable indexOfObjectIdenticalTo:dInner]; 
      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:1]]; 
       [self.arForTable insertObject:dInner atIndex:count++]; 
      } 
      [tableView insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationTop]; 
     } 
    } 
} 

也許代碼需要一些ajustments,而是看它是否能幫助你。

0

你切切實實的可擴展和使用HVTableView

這是UITableView的與展開/摺疊功能,一個子類, 自帶有用容易塌陷細胞在很多情況下。