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

2023-12-01

我正在尝试以手风琴单元格模式展开/折叠表格视图单元格,这样,当用户点击该行时,他们将通过扩展行高来获得单元格内所选行的详细描述。我有 2 个数组,“array”和“detailarray”,分别用于在“cell.textLabel.text”和“cell.detailTextLabel.text”单元格中显示它。现在最初我已将“cell.detailTextLabel.hidden = YES”设置为隐藏,以便当用户点击该行时可以获得单元格的扩展文本。

My code,

- (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 函数停止时它会消失,为什么会发生这种情况,任何人都可以在这方面帮助我。


查看SubTable,它会为您处理扩展/折叠。

如果您正在寻找要在主单元格下显示的单个详细信息单元格,您可以像这样实现委托方法:

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

并根据您的需求自定义单元格的高度和外观

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

手风琴表格单元格 - 用于展开和折叠 ios 的相关文章

随机推荐