如何在单击按钮时增加单元格大小或调整大小?

时间:2013-09-04 10:20:26

标签: ios uitableview

我有一个自定义表视图,在行单元格中为行索引路径动态包含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;

1 个答案:

答案 0 :(得分: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重新加载相应的行:

  4. 这将调用此行的数据源方法(tableView:heightForRowAtIndexPath:和tableView:CellForRowAtIndexPath :),您的单元格将使用withRowAnimation:parameter中指定的动画增加它的高度。