2013-03-15 20 views
4

我正在開發應用程序,需要使用DDMenuController幫助的Facebook/Gmail iphone應用程序類型菜單。但是現在我已經要求其中一行需要在另一個帶有5行的tableview的時候顯示手風琴(所有行應該是可點擊的並且能夠推送新的viewcontroller)。 我已經看過幾個代碼示例,但似乎沒有什麼符合我的要求,所以只是試圖在這裏把它出來,希望有人有更好的解決方案。如何在UItableview下使用UItableview創建手風琴?

感謝,

+4

5秒

代碼在谷歌帶來了不少像樣的教程。像[this](http://www.cocoanetics.com/2011/03/expandingcollapsing-tableview-sections/)一樣。只需嘗試搜索展開式或摺疊式表格。 – SethHB

+0

您可以在Swift中查看這個手風琴示例:https://github.com/tadija/AEAccordion 它只有很少的代碼來創建手風琴效果(不是通過使用分區而是單元),作爲獎勵,也是在其他XIB文件內使用XIB文件的解決方案(對於使用自定義視圖的自定義單元格很有用)。 – tadija

回答

8

更好的辦法是展開或摺疊的TableView第

很好的教程,請here

您可以下載示例代碼here

示例代碼

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    // Configure the cell... 

    if ([self tableView:tableView canCollapseSection:indexPath.section]) 
    { 
     if (!indexPath.row) 
     { 
      // first row 
      cell.textLabel.text = @"Expandable"; // only top row showing 

      if ([expandedSections containsIndex:indexPath.section]) 
      { 
       cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp]; 
      } 
      else 
      { 
       cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeDown]; 
      } 
     } 
     else 
     { 
      // all other rows 
      cell.textLabel.text = @"Some Detail"; 
      cell.accessoryView = nil; 
      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
     } 
    } 
    else 
    { 
     cell.accessoryView = nil; 
     cell.textLabel.text = @"Normal Cell"; 

    } 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if ([self tableView:tableView canCollapseSection:indexPath.section]) 
    { 
     if (!indexPath.row) 
     { 
      // only first row toggles exapand/collapse 
      [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

      NSInteger section = indexPath.section; 
      BOOL currentlyExpanded = [expandedSections containsIndex:section]; 
      NSInteger rows; 

      NSMutableArray *tmpArray = [NSMutableArray array]; 

      if (currentlyExpanded) 
      { 
       rows = [self tableView:tableView numberOfRowsInSection:section]; 
       [expandedSections removeIndex:section]; 

      } 
      else 
      { 
       [expandedSections addIndex:section]; 
       rows = [self tableView:tableView numberOfRowsInSection:section]; 
      } 

      for (int i=1; i<rows; i++) 
      { 
       NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i 
                   inSection:section]; 
       [tmpArray addObject:tmpIndexPath]; 
      } 

      UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

      if (currentlyExpanded) 
      { 
       [tableView deleteRowsAtIndexPaths:tmpArray 
           withRowAnimation:UITableViewRowAnimationTop]; 

       cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeDown]; 

      } 
      else 
      { 
       [tableView insertRowsAtIndexPaths:tmpArray 
           withRowAnimation:UITableViewRowAnimationTop]; 
       cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp]; 

      } 
     } 
    } 
} 
+0

這不是一個擴大,它的一個該死的警報大聲笑 – ChuckKelly

12

我的工作之一是要求有一個手風琴視圖,但有多個級別的單元展開和摺疊,像打開/關閉目錄結構一樣。

我做了一個樣本,我能夠達到預期的效果。基本概念是一樣的,我只使用deleteRowsAtIndexPath和insertRowsAtIndex路徑,但是通過創建一個具有父子關係的模型對象,並在父母被輕敲時將子元素加載到主數組中。我不擅長編寫教程,所以我分享我的示例代碼,希望它可以幫助某人。這裏Accordion_git

代碼更新 做了這樣的SWIFT版本,不知道是最佳的,但它的工作原理。這裏Accordion_SWIFT

Accordion

+2

什麼是一個不好的選擇共享網站.. – Rizon

+2

@ Rizon改爲git :) – anoop4real

+1

謝謝!幾年前我使用嵌套的tableviews實現了這個控件,但是你的方法更有意義。乾杯。 – codrut