手风琴表格单元格 – 用于展开和折叠ios

我正在尝试用手风琴单元格模式展开/折叠桌面单元格,这样,当用户点击该行时,通过扩展行高就可以得到单元格内所选行的详细描述。 我有2个数组,'数组'和'detailarray',用于在'cell.textLabel.text'和'cell.detailTextLabel.text'单元格中显示它。 现在最初我已经把'cell.detailTextLabel.hidden = YES'作为隐藏的,这样当用户点击行时就可以得到单元格的扩展文本。

我的代码,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; // Configure the cell... cell.textLabel.text=[array objectAtIndex:indexPath.row]; cell.detailTextLabel.text=[detailarray objectAtIndex:indexPath.row]; cell.detailTextLabel.hidden = YES; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; NSLog(@"indexpath is %d", indexPath.row); selectedIndex = indexPath.row; isSearching = YES; [self.tableview beginUpdates]; static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; cell.textLabel.text=[array objectAtIndex:indexPath.row]; cell.detailTextLabel.text=[detailarray objectAtIndex:indexPath.row]; cell.detailTextLabel.hidden = NO; [self.tableview endUpdates]; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (isSearching && indexPath.row == selectedIndex) { return 77; } return 44; } 

现在我创build了一个4行的tableviewcell,当每一行点击时,我会得到所选行的扩展视图。 在运行期间,我可以在扩展时看到单元格上的“detailarray”文本,但是当didselectRowAtIndexPath函数停止时,它会消失,为什么会发生这种情况,任何人都可以在这方面帮助我。

看看子表 ,它为你处理展开/折叠。

如果您正在寻找一个单独的细节单元格来显示主单元格,您可以像这样实现委托方法:

 - (NSInteger)numberOfChildCellsUnderParentIndex:(NSInteger)parentIndex { if (detailarray[parentIndex] contains content) return 1; else return 0; } 

并自定义单元格的高度,并看起来符合您的需求

select单元格后,请记住它在selectedIndex属性中的索引(您必须创build一个)。

添加function到你的表格视图:

 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return indexPath.row == selectedIndex ? extendedHeight : normalHeight; } 

select单元格后,不要忘记重新加载tableView。

Interesting Posts