显示数据的可扩展UITableView单元格

我有一些按年份编目的运动比赛,每场比赛我都有最终结果,比赛日期和得分手。

我在’表格视图’中显示这些匹配,如下所示:

在此处输入图像描述

所以我想要实现的是:当点击一个单元格时,显示匹配的详细信息,如图所示。

我还发现了一些库来实现手风琴/可扩展的风格,但没有人能完成这项任务。 他们只是扩展细胞并显示另一个细胞。

在这种情况下,您甚至不需要使用可扩展/手风琴。 以下是解决这个问题的方法。 让我们说正常情况下你的单元格大小为40,特别点击时单元格为100.在heightForRowAtIndexPath中,你可以检查选择了哪个单元格并返回更多高度

if(selectedRow == indexPath.row) { return 100; } else { return 40; } 

然后你可以做的是在didSelectRowAtIndexPath或clickEvent方法

 [self.tableView beginUpdates]; [[self tableView] reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForItem: selectedRow inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic]; [self.tableView endUpdates]; 

因此,您的单元格将被渲染所有内容,但基于您隐藏或显示内容的高度。

使用输入源更新了答案

ViewController.m

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if(selectedIndex == indexPath.row) return 100; else return 40; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { customCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"Cell"]; NSDictionary *matches = self.dataArray[indexPath.row]; cell.matchName.text = [matches objectForKey@"MatchName"]; if() { cell.heightConstraintSecondView.constant = 0; } else { cell.heightConstraintSecondView.constant = 59; cell.team1.text = [matches objectForKey@"Team1"]; cell.team2.text = [matches objectForKey@"Team2"]; cell.score.text = [matches objectForKey@"Score"]; } return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { selectedIndex = indexPath.row; [self.tableView beginUpdates]; [[self tableView] reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; [self.tableView endUpdates]; } } 

您可以为此目的使用HVtableview,试试这个链接https://github.com/xerxes235/HVTableView

更简洁的方法是创建具有不同标识符的2个单元格,并在单击单元格时在运行时更改它。 检查此问题: iOS中UITableView中的下拉列表

尝试以下方法之一:

  • obj-C: KMAccordionTableViewController
  • swift: AccordionTableViewController