在ios中展开和折叠表格视图单元格

我有一个单元格的自定义单元格和一些button的表格视图。单击单元格内的任何button将显示该单元格下方的另一个自定义视图。接下来单击同一个button将折叠视图,并需要相同的所有单元格。我试图用insertrow方法点击button,但徒劳无益。如何使用只有表视图代表这样做。

这是我试过的:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *simpleTableIdentifier = @"CustomCell_For_Dashboard"; CustomCellFor_Dashboard *customCell = (CustomCellFor_Dashboard *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; if (customCell == nil) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCellFor_Dashboard" owner:self options:nil]; customCell = [nib objectAtIndex:0]; } [customCell.howyoulfeelBtn addTarget:self action:@selector(buttonclicked:) forControlEvents:UIControlEventTouchUpInside]; customCell.nameLabel.text = @"test"; customCell.imgView.image = [UIImage imageNamed:@"Default.png"]; // customCell.prepTimeLabel.text = [prepTime objectAtIndex:indexPath.row]; return customCell; } -(void)buttonclicked:(id)sender{ NSIndexPath *indexPath = [myTable indexPathForCell:sender]; [myTable beginUpdates]; NSIndexPath *insertPath = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]; [myTable insertRowsAtIndexPaths:[NSArray arrayWithObject:insertPath] withRowAnimation:UITableViewRowAnimationTop]; } 

谁能帮我?

我在一个项目上得到了相同的任务,只有一件事情不同:没有button,只要点击单元格就会展开或折叠它。

在代码中应该编辑几件事情。 首先,button方法代码如下所示:

 - (void) collapseExpandButtonTap:(id) sender { UIButton* aButton = (UIButton*)sender; //It's actually a button NSIndexPath* aPath = [self indexPathForCellWithButtonTag:aButton.tag]; //Let's assume that you have only one section and button tags directly correspond to rows of your cells. //expandedCells is a mutable set declared in your interface section or private class extensiont if ([expandedCells containsObject:aPath]) { [expandedCells removeObject:aPath]; } else { [expandedCells addObject:aPath]; } [myTableView beginEditing]; [myTableView endEditing]; //Yeah, that old trick to animate cell expand/collapse } 

现在第二件事是UITableViewDelegate方法:

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if ([expandedCells containsObject:indexPath]) { return kExpandedCellHeight; //It's not necessary a constant, though } else { return kNormalCellHeigh; //Again not necessary a constant } } 

这里的关键是确定你的单元格是否应该展开/折叠,并在委托方法中返回正确的高度。

离开@ eagle.dan.1349说什么,这是如何做到这一点点击的单元格。 在故事板中,还需要将表格单元格设置为剪辑子视图,否则将显示隐藏的内容。

。H

 @property (strong, nonatomic) NSMutableArray *expandedCells; 

.M

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if ([self.expandedCells containsObject:indexPath]) { [self.expandedCells removeObject:indexPath]; } else { [self.expandedCells addObject:indexPath]; } [tableView beginUpdates]; [tableView endUpdates]; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { CGFloat kExpandedCellHeight = 150; CGFloat kNormalCellHeigh = 50; if ([self.expandedCells containsObject:indexPath]) { return kExpandedCellHeight; //It's not necessary a constant, though } else { return kNormalCellHeigh; //Again not necessary a constant } } 

看到这个post,只是想给我的2美分,因为我的解决scheme,这是非常类似于所选的答案(窃听整个地区)。

许多人通过单独使用单元来devise这种方法,但我相信有一种方法可以使这种方法更好地与人们试图实现的方法保持一致:

标题 ,有细胞标题应该是可打的 ,然后标题下的单元格显示或隐藏 。 这可以通过添加一个手势识别器到头部来实现,当点击时,你只需要移除该头部(该部分)下面的所有单元格,反之亦然(添加单元格)。 当然,你必须保持哪些标题是“打开的”,哪个标题是“closures的”状态。

这是很好的几个原因:

  1. 标题和单元格的作业是分开的,这使得代码更清晰。
  2. 这个方法可以很好的与表格视图(标题和单元格)结合起来,因此没有太多的魔力 – 代码只是删除或添加单元格,而且应该与更高版本的iOS兼容。

我做了一个非常简单的库来实现这一点。 只要你的表格视图是用UITableView节头和单元格设置的, 你所要做的就是对tableview和header进行子类化。

链接: https : //github.com/fuzz-productions/FZAccordionTableView

在这里输入图像说明

我也有一个相同的情况,我的解决scheme是用viewForHeaderInSection方法把一个button上的部分标题。

noOfRows定义每个部分有多less行, button.tag保持按下哪个部分的button。

 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIButton *btnSection = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, tableView.frame.size.height)]; btnSection.tag = section; [btnSection setTitle:[sectionArray objectAtIndex:section] forState:UIControlStateNormal]; [btnSection addTarget:self action:@selector(sectionButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; return btnSection; } - (void)sectionButtonTapped:(UIButton *)button { sectionIndex = button.tag; if (button.tag == 0) { noOfRows = 3; } else if (button.tag == 1) { noOfRows = 1; } else if (button.tag == 2) { noOfRows = 2; } [self.tableView reloadData]; } 

希望对你有帮助..