折叠式表格单元-用于展开和折叠ios

折叠式表格单元-用于展开和折叠ios,ios,uitableview,expand,collapse,Ios,Uitableview,Expand,Collapse,我正在尝试以手风琴单元格模式展开/折叠tableview单元格,这样,当用户点击该行时,他们将通过扩展行高度获得单元格中所选行的详细描述。我有两个数组,“array”和“detailarray”分别用于在“cell.textlab.text”和“cell.detailtextlab.text”单元格中显示它。现在,我已经将“cell.detailTextLabel.hidden=YES”设置为隐藏,这样当用户点击该行时,就可以获得单元格的扩展文本 我的代码 - (UITableViewCell

我正在尝试以手风琴单元格模式展开/折叠tableview单元格,这样,当用户点击该行时,他们将通过扩展行高度获得单元格中所选行的详细描述。我有两个数组,“array”和“detailarray”分别用于在“cell.textlab.text”和“cell.detailtextlab.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;    
}
现在我已经创建了一个包含4行的tableviewcell,当点击每行时,我将获得所选行的扩展视图。
在运行期间,当单元格扩展时,我可以在单元格上看到“detailarray”文本,但当didselectRowAtIndexPath函数停止时,它会消失。为什么会发生这种情况,任何人都可以在这方面帮助我。

选择单元格后,请记住其在selectedIndex属性中的索引(您必须创建一个)

将函数添加到表视图:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return indexPath.row == selectedIndex ? extendedHeight : normalHeight;
}
选择单元格后不要忘记重新加载tableView。

签出,它为您处理展开/折叠

如果要在主详细信息单元格下显示单个详细信息单元格,可以实现委托方法,如下所示:

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

自定义单元格的高度并根据您的需要查看

实际上,我可以获得单元格的扩展视图,问题是在选择显示detailarray的行时,一旦我停止按该行,detailarray文本将消失,但扩展行的高度仍与100相同。这是与您的相同的。您还可以使用以下教程进行演示;1) 2)3)希望这会有所帮助。