2014-02-12 89 views
1

我現在tableview擴展單元上點擊,例如:可擴展的tableview細胞

Parent 0 

    -Child 1 

    -Child 2 

    -Child 3 

什麼我掙扎做的是,當我展開一個小區的所有其他人將關閉,我試圖讓當然只有一個單元格是開放的。你們可以提供一些關於如何做的建議嗎?

電流擴展單元代碼:

- (void)tableView:(UITableView *)tableView1 didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
if (indexPath.section==0) { 

    if([d valueForKey:@"produtos"]) { 
     NSArray *ar=[d valueForKey:@"produtos"]; 

     BOOL isAlreadyInserted=NO; 

     for(NSDictionary *dInner in ar){ 
      NSInteger index=[self.firstForTable indexOfObjectIdenticalTo:dInner]; 
      isAlreadyInserted=(index>0 && index!=NSIntegerMax); 
      if(isAlreadyInserted) break; 
     } 

     if(isAlreadyInserted) { 
      [self miniMizeFirstsRows:ar]; 
     } else { 
      NSUInteger count=indexPath.row+1; 
      NSMutableArray *arCells=[NSMutableArray array]; 
      for(NSDictionary *dInner in ar) { 
       [arCells addObject:[NSIndexPath indexPathForRow:count inSection:0]]; 
       [self.firstForTable insertObject:dInner atIndex:count++]; 
      } 

      [tableView1 beginUpdates]; 
      [tableView1 insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationNone]; 
      [tableView1 endUpdates]; 
      [tableView1 scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES]; 

     } 

    }else{ 
     NSLog(@"child %@ %@|",[item valueForKey:@"nome"],[item valueForKey:@"produtos"]); 
    } 
} 

儘量減少電池電流代碼:

​​

在此先感謝。

EDIT仍然不能使它工作 我有什麼,通過使用從馬可回答的幫助:

NSLog(@"indexPath1 = %i",selectedRow); 

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

      NSUInteger count=indexPath.row +1; 
      NSMutableArray *arCells=[NSMutableArray array]; 
      for(NSDictionary *dInner in ar) { 
       [arCells addObject:[NSIndexPath indexPathForRow:count inSection:0]]; 
       [self.firstForTable insertObject:dInner atIndex:count++]; 
      } 
      [tableView insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationLeft]; 
      // } 
     }else 
     { 
      NSLog(@"Leave Element:::%@ %@|",[d valueForKey:@"name"],[d valueForKey:@"book"]); 
     } 

    // The user is selecting the cell which is currently expanded 
    // we want to minimize it back 
    if (selectedRow == row) 
    { 

     NSLog(@"selectedRow2 = %i",selectedRow); 

     NSDictionary *d=[self.firstForTable objectAtIndex:selectedRow]; 
     if([d valueForKey:@"Objects"]) { 
      NSArray *ar=[d valueForKey:@"Objects"]; 

      [self miniMizeFirstsRows:ar]; 

     } 

     selectedRow = -1; 

     return; 
    } 

    // First we check if a cell is already expanded. 
    // If it is we want to minimize make sure it is reloaded to minimize it back 
    if (selectedRow >= 0) 
    { 

     NSLog(@"selectedRow3 = %i",selectedRow); 

     NSDictionary *d=[self.firstForTable objectAtIndex:selectedRow]; 
     if([d valueForKey:@"Objects"]) { 
      NSArray *ar=[d valueForKey:@"Objects"]; 

      [self miniMizeFirstsRows:ar]; 

     } 
     selectedRow = row; 
    } 

    // Finally set the selected index to the new selection and reload it to expan 
    selectedRow = row; 
    [tableView beginUpdates]; [tableView endUpdates]; 
} 

一些更多的幫助,請:)

+0

檢查此https://www.cocoacontrols.com/controls/ratreeview – caglar

+0

謝謝:)但控制是複雜的方式,我的需要,它擴大了多個單元格,希望不是我想要的行爲。我希望當時擴大一個細胞,當我擴大一個細胞時,所有其他細胞應該關閉。 – Bruno

回答

-1

你需要一個實例變量跟蹤您選擇的單元格。

注:下面的碼不意味着取代您的tableview數據源和委託方法,但作爲一個例子來跟蹤所選擇的單元:

一個NSInteger selectedRow實例變量添加到你的視圖控制器。

viewDidLoad,初始化selectedRow表示沒有細胞將擴大:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Set set our selectedRow to -1 to indicate no cell will be expanded 
    selectedRow = -1; 
} 

tableView:didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{  
    NSInteger row = indexPath.row; 

    // The user is selecting the cell which is currently expanded 
    // we want to minimize it back 
    if (selectedRow == row) 
    { 
     selectedRow = -1; 
     [tableView beginUpdates]; [tableView endUpdates]; 

     return; 
    } 

    // First we check if a cell is already expanded. 
    // If it is we want to minimize make sure it is reloaded to minimize it back 
    if (selectedRow >= 0) 
    { 
     selectedRow = row; 
    } 

    // Finally set the selected index to the new selection and reload it to expand 
    selectedRow = row; 
    [tableView beginUpdates]; [tableView endUpdates]; 
} 
+0

如果您正在更改的唯一一項是單元格高度,重新加載行將導致閃爍效果,因爲內容將相同。你可以做'[tableView beginUpdates]; [tableView endUpdates];'不重新加載任何行,任何高度變化將自動生成動畫。 –

+0

@AaronBrager你是對的,相應地更新/簡化代碼。謝謝。 – Marco

+0

感謝Marco和Aaron的回答,但我仍然無法完成工作,我編輯了主要帖子來展示我目前擁有的內容。 – Bruno

1

一件事從馬可的答案缺少的是 「heightForRowAtIndexPath」 代表法的實施。 因此,這些都是你需要遵循以下步驟:

  1. 創建一個變量來記錄所選擇的行數變量當行被選中(在「didSelectRowAtIndexPath方法」委託方法)
  2. 更新,並調用的tableView的beginUpdates & endUpdates方法。
  3. 返回更大高度時indexPath.row == selectedRow,否則返回正常高度。