2013-02-27 48 views
11

我想展開tableview單元格單擊它。有一個tableview,其中兩個單元格可擴展,其他單元格不可擴展。我需要當我點擊可展開的單元格時,它應該顯示它下面的單元格,並再次點擊它應該隱藏。下面有一張圖片定義了這一點。展開UITableViewstylePlain中的UITableView單元格

enter image description here

我怎樣才能實現這個功能,請指導的上方。 在此先感謝。

+0

你的問題解決了嗎? – Bhavin 2013-02-27 10:16:29

+0

不,還沒有... – 2013-02-27 10:17:34

+0

你檢查了我的答案嗎? – Bhavin 2013-02-27 10:18:20

回答

12

Here是可擴展的UITableView

完整的教程下面是該代碼剪斷。

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

      } 
     } 
    } 
} 

如下圖所示畫面

enter image description here

Thisthis一個解決方案還可以幫助您瞭解如何管理可擴展的TableView

+0

這是組表,我只是要求普通表。 – 2013-02-27 10:14:41

+0

@iPhone是否爲普通表格檢查了[this](http://www.roostersoftstudios.com/2011/04/14/iphone-uitableview-with-animated-expanding-cells/) – swiftBoy 2013-02-27 10:19:21

+0

我以前見過這樣的示例發佈的問題,這個使用部分擴大了,原來我們只有一節只有cell需要擴展。 – 2013-02-27 10:21:13

3

我想你想要的是Apple給出的。您可以在表格視圖動畫和手勢的標題下看看蘋果提供的Sample Code

0

這可能是由委託

- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath { 
     //write code here 
    } 
+0

你有沒有使用這種方法來達到這個目的,你可以發佈這個樣本代碼 – 2013-02-27 10:33:47

4

好了,我在想什麼是,標題,活動開銷朋友將自定義UIViewUIImageView背景,UILabel爲標題,並UIButton的擴大。所以基本上

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 

有你的標題返回計數。有

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 

每個標題回報UIView

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 

具有該標題內的行數。 您可能需要爲此維護一個數組。具有細胞項

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 

最初的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; 

它應該是0(零)的所有部分中,當用戶敲擊擴大,它應該是相對於增加的行數*該部分中的單元格高度

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

你可能需要一些良好的邏輯來設置所有行的擴張

您的擴展按鈕的操作類似,

- (void) expandSection:(UIButton *)sender; 

,你可以找出哪些部分使用sender.tag被擴大,所以不要忘記正確添加標籤。您可能需要int中的.h文件來存儲當前段,並且可以使用- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section數據源方法。

2

我給出了下面的示例源代碼。您可以根據您的要求自定義此示例。

源代碼鏈接:Click here

輸出畫面:

enter image description here

0

接受的答案是正確的,但該鏈接已經過期。爲了讓這個例子工作,你必須做一些額外的事情:

下載ExpandableTableCells項目從這裏:
https://github.com/cocoanetics/Examples

下載DTCustomColoredAccessory.h和.m文件從這裏: https://github.com/Cocoanetics/DTFoundation/tree/develop/Core/Source/iOS

把DTCustomColoredAccessory ExpandableTableCells項目中的文件。

在那裏你有一個工作的例子,它更容易編輯和從那裏移動,而不是試圖從零寫入。

+0

我已經下載了'Download ExpandableTableCells項目'並添加了下載DTCustomColoredAccessory.h和.m文件。但我得到一個錯誤「錯誤:鏈接器命令失敗,退出代碼1(使用-v來查看調用)」你能幫忙嗎? – 2015-01-26 13:33:47

+0

你能在錯誤中發佈更多細節嗎? – Esqarrouth 2015-01-26 13:37:33