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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18607722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:58:55  来源:igfitidea点击:

Accordion table cell - for expand and collapse ios

iosuitableviewexpandcollapse

提问by sathya

I am trying to expand/collapse of tableview cells in accordion cell pattern, So that, when a user taps the row they would get the detailed description of the selected row within the cell by extending the row height. I have 2 arrays, 'array' and 'detailarray' for displaying it in 'cell.textLabel.text' and 'cell.detailTextLabel.text' cells resp. now initially i have made 'cell.detailTextLabel.hidden = YES' as hidden, so that when user taps the row can get the extended text of the cell.

我正在尝试以手风琴单元格模式展开/折叠 tableview 单元格,这样,当用户点击该行时,他们将通过扩展行高来获得单元格内所选行的详细描述。我有 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;    
}

Now I have created a tableviewcell with 4 rows and when tapping each row i would get the extended view of the selected row. During runtime I could see the 'detailarray' text on my cell when it extends, but it disappears when didselectRowAtIndexPath function is stopped, why is that happening so, any one can help me in this regards.

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

回答by Alex Koshy

Check out SubTable, it handles the expanding/collapsing for you.

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

If you're looking for a single detail cell to display under the main one, you can implement the delegate method like so:

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

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

and customize the cell's height and looks to your needs

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

回答by ninja_iOS

After selecting cell remember its index in selectedIndex property (you must make one).

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

Add function to your table view:

将函数添加到您的表视图:

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

Don't forget to reload tableView after selecting cell.

选择单元格后不要忘记重新加载 tableView。