在 ios 中展开和折叠表格视图单元格 [英] Expanding and Collapsing table view cells in ios

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

问题描述

我有一个自定义单元格的表格视图和每个单元格中的一些按钮.单击单元格内的任何按钮将显示该单元格下方的另一个自定义视图.下次单击同一按钮将折叠视图并需要相同的对于所有单元格.我尝试在按钮单击时使用 insertrow 方法,但徒劳无功.如何仅使用表格视图委托来做到这一点.

I have a table view of custom cells and some buttons in each cell.Clicking on any of the button inside the cell will reveal another custom view below that cell.Next click on the same button will collapse the view and need this same for all cells.I tried with insertrow method on the button click but in vain.How can i do this with using only the table view delegates.

这是我试过的:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"CustomCell_For_Dashboard";

    CustomCellFor_Dashboard  *customCell = (CustomCellFor_Dashboard *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (customCell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCellFor_Dashboard" owner:self options:nil];
        customCell = [nib objectAtIndex:0];
    }
    [customCell.howyoulfeelBtn  addTarget:self action:@selector(buttonclicked:) forControlEvents:UIControlEventTouchUpInside];
      customCell.nameLabel.text = @"test";
    customCell.imgView.image = [UIImage imageNamed:@"Default.png"];
   // customCell.prepTimeLabel.text = [prepTime objectAtIndex:indexPath.row];
    return customCell;
}

-(void)buttonclicked:(id)sender{
    NSIndexPath *indexPath = [myTable indexPathForCell:sender];


    [myTable beginUpdates];

     NSIndexPath *insertPath = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
   [myTable insertRowsAtIndexPaths:[NSArray arrayWithObject:insertPath] withRowAnimation:UITableViewRowAnimationTop];
  } 

有人可以帮我吗?

推荐答案

我在一个项目上得到了相同的任务,只是有一点不同:没有按钮,只需点击单元格就会展开或折叠它.

I got the same task on one project with just one thing different: There were no buttons, just tapping on cell will expand or collapse it.

您应该在代码中编辑几项内容.首先,按钮方法代码如下所示:

There are several things you should edit in your code. First, the button method code will look something like this:

- (void) collapseExpandButtonTap:(id) sender
{
    UIButton* aButton = (UIButton*)sender; //It's actually a button
    NSIndexPath* aPath = [self getIndexPathForCellWithButtonByMagic:aButton];
    //expandedCells is a mutable set declared in your interface section or private class extensiont
    if ([expandedCells containsObject:aPath])
    {
        [expandedCells removeObject:aPath];
    }
    else
    {
        [expandedCells addObject:aPath];
    }
    [myTableView beginEditing];
    [myTableView endEditing]; //Yeah, that old trick to animate cell expand/collapse
}

现在第二件事是UITableViewDelegate方法:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([expandedCells containsObject:indexPath])
    {
        return kExpandedCellHeight; //It's not necessary a constant, though
    }
    else
    {
        return kNormalCellHeigh; //Again not necessary a constant
    }
}

这里的关键是确定您的单元格是否应该展开/折叠并在委托方法中返回正确的高度.

Key thing here is to determine if your cell should be expanded/collapsed and return right height in delegate method.

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

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