0

我有一个自定义表格视图,在单元格中动态包含 2 个按钮,用于行索引路径,

我想当我点击增加尺寸时应该增加并且按钮应该隐藏,并且

然后应该可以看到一个用于调整行高大小的按钮 - 提前致谢。

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 


    return JcOutCellSize;

    }  


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

}
// ----------------------- cellbackground image-----------------------------   

    cell.backgroundView =[[UIImageView alloc]initWithFrame:CGRectMake(160, 151, 320, 108)];
    cell.backgroundView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"inCellFullBg.png"]]; 

//***********  Change Cell BackGround  Colour*****


    // cell.contentView.backgroundColor = [UIColor darkGrayColor];







//----------------------------Cell buttons-------------------------------------------  

     if (JcOutCellSize==152)
     {
    UIButton *AddComment = [UIButton buttonWithType:UIButtonTypeCustom]; // custom means transparent it takes shape same with backgroup image
    [AddComment addTarget:self 
                   action:@selector(TestBtns:)
         forControlEvents:UIControlEventTouchDown];
    // [AddComment setTitle:@"1" forState:UIControlStateNormal];
    AddComment.frame = CGRectMake(9.0, 128.0, 96.0, 26.0);
    [cell.contentView addSubview:AddComment];

    UIImage *buttonAddComment = [UIImage imageNamed:@"addcomment.png"];
    [AddComment setBackgroundImage:buttonAddComment forState:UIControlStateNormal];
    [cell.contentView addSubview:AddComment]; 

     }


    if (JcOutCellSize==152)
    {
        NSLog(@" up arrow %f",JcOutCellSize);
        UIButton *UpArrow = [UIButton buttonWithType:UIButtonTypeCustom];
        [UpArrow addTarget:self 
                    action:@selector(ResizeCell:)
          forControlEvents:UIControlEventTouchDown];

        //[DownArrow setTitle:@"Arrow" forState:UIControlStateNormal];
        UpArrow.frame = CGRectMake(143.0, 136.0, 26.0, 16.0);
        [cell.contentView addSubview:UpArrow];

        UIImage *buttonUp = [UIImage imageNamed:@"upArrow.png"];
        [UpArrow setBackgroundImage:buttonUp forState:UIControlStateNormal];
        [cell.contentView addSubview:UpArrow];   
        NSLog(@" cell size = %f",JcOutCellSize);
    }
    //----------------------------------------------------------------------------------    

    if (JcOutCellSize==132)
    {
        NSLog(@" down arrow %f",JcOutCellSize);
        UIButton *DownArrow = [UIButton buttonWithType:UIButtonTypeCustom];
        [DownArrow addTarget:self 
                      action:@selector(IncreseCellHight:)
            forControlEvents:UIControlEventTouchDown];

        //[DownArrow setTitle:@"Arrow" forState:UIControlStateNormal];
        DownArrow.frame = CGRectMake(143.0, 116.0, 26.0, 16.0);
        [cell.contentView addSubview:DownArrow];

        UIImage *buttondown = [UIImage imageNamed:@"upDownArrow.png"];
        [DownArrow setBackgroundImage:buttondown forState:UIControlStateNormal];
        [cell.contentView addSubview:DownArrow]; 
        NSLog(@" cell size = %f",JcOutCellSize);
    }




       return cell;
    }

鉴于 didload 我已经完成了这个 JcOutCellSize=152;

4

1 回答 1

0

要通过单击按钮更改行高,您应该执行以下步骤: 1. 创建一些标志来指示展开哪一行,例如 _expandedIndexPath

  1. 像这样实现正确的数据源方法:

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath { 
    
    if (_expandedIndexPath isEqual:indexPath) {
        return JcBigCellSize; // large cell
    }
    else
        return JcOutCellSize; // normal cell
    }  
    
  2. 要定义按下哪个单元格按钮,请将按钮的标记设置为相应的 indexPath.row(在 tableView:cellForRowAtIndexPath 中:)

  3. 在按钮操作 (IncreseCellHight:) 上获取按钮的标签,将 _expandedIndexPath 设置为您希望看到展开的索引路径(很可能是 [NSindexPath indexPathForRow:sender.tag inSection:0])并使用 UITableView 的方法 reloadRowsAtIndexPaths 重新加载相应的行: withRowAnimation:

这将为此行调用数据源方法(tableView:heightForRowAtIndexPath: 和 tableView:CellForRowAtIndexPath:),并且您的单元格将通过 withRowAnimation: 参数中指定的动画增加其高度。

于 2013-09-04T11:23:14.910 回答