ios - 在 ios 中展开和折叠表格 View 单元格

标签 ios objective-c uitableview customization

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

这是我尝试过的:

- (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];
  } 

谁能帮帮我?

最佳答案

我在一个项目上完成了相同的任务,只有一点不同:没有按钮,只需点击单元格即可展开或折叠它。

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

- (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
    }
}

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

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

相关文章:

ios 11 attributeString 不适用于粗体文本

ios - Swift:如何关闭从 AppDelegate 启动的 View Controller 和导航 Controller

objective-c - 核心数据中关系的默认值

iphone - 在未完成的方法内运行方法

ios - UIcollectionview 多选取消选择单元格

java - 如何为 iOS 推送通知传递 JSON 对象

ios - 如何启用 MPMediaItemArtwork 下一个/上一个按钮?

ios6 - 调用 beginRefreshing 且 contentOffset 为 0 时,UIRefreshControl 不显示刺状

iphone - UITableView pushViewController 在选择一行时

iphone - 为 OBShapedButton 的(自定义形状按钮)图像着色并重新设置