2013-02-27 63 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