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

标签 ios uitableview

我正在尝试以 Accordion 单元格模式展开/折叠表格 View 单元格,这样,当用户点击该行时,他们将通过扩展行高来获得单元格内所选行的详细描述。我有 2 个数组,“array”和“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;    
}

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

最佳答案

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

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

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

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

关于ios - Accordion 表格单元格 - 用于展开和折叠 ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18607722/

相关文章:

ios - swift : Button not responding to tap

ios - 如何导航 UIViewController 作为以编程方式创建的 UIButton 操作的目标

ios - iOS 上的自定义模板系统

带有 3 个原型(prototype)单元和自动高度的 Swift tableView

ios - 在 Swift 中不使用 didSelectRowAtIndexPath 在 DetailView 中引用特定于单元格的 JSON 键

ios - UITableViewCell 中的 UIPickerView 显示在下一个 Cell 后面

objective-c - UITableView : popViewController and keep row index into parent controller?

ios - Swift:如何无延迟地刷新 tableview (UIRefreshControl)

iphone - 查看 NSString drawInRect 将使用多少行的简单方法?

ios - XXX 不支持 Azure 构建管道中的配置文件