手风琴表格单元格 - 用于展开和折叠ios [英] Accordion table cell - for expand and collapse ios

查看:116
本文介绍了手风琴表格单元格 - 用于展开和折叠ios的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以手风琴单元格模式展开/折叠tableview单元格。因此,当用户点击该行时,他们将通过扩展行高度获得单元格中所选行的详细描述。我有2个数组,'array'和'detailarray',用于在'cell.textLabel.text'和'cell.detailTextLabel.text'单元格中显示它。现在我最初把'cell.detailTextLabel.hidden = YES'隐藏起来,这样当用户点击行时就可以获得单元格的扩展文本。

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.

我的代码,

- (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,当我点击每行时,我会获取所选行的扩展视图。
在运行期间,当我的单元格延伸时,我可以在单元格上看到'详细说明'文本,但是当didselectRowAtIndexPath函数停止时,它会消失,为什么会发生这种情况,任何人都可以在这方面帮助我。

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.

推荐答案

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

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

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

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

这篇关于手风琴表格单元格 - 用于展开和折叠ios的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆