如何在iOS中按钮单击后以编程方式增加单元格高度

时间:2014-01-28 05:12:55

标签: ios objective-c uitableview

我正在使用TableviewController。我使用自定义单元格有一个回复按钮。当我点击回复按钮,然后我想在同一个单元格中添加视图。此视图正在添加,但此单元格高度不会增加。如何在iOS中按下按钮后以编程方式增加单元格高度。我使用了以下代码。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   if (indexPath.row==buttontag1)
   {
        pppp=[postText objectAtIndex:indexPath.row];
        CGSize maxSize = CGSizeMake(280, MAXFLOAT);//set max height
        CGSize cellSize = [pppp sizeWithFont:[UIFont systemFontOfSize:14]
        constrainedToSize:maxSize lineBreakMode:NSLineBreakByWordWrapping];

         //this will return correct height for text
          return cellSize.height+150;
   }
   else
   {
        pppp=[postText objectAtIndex:indexPath.row];
        CGSize maxSize = CGSizeMake(280, MAXFLOAT);//set max height
        CGSize cellSize = [pppp sizeWithFont:[UIFont systemFontOfSize:14]
        constrainedToSize:maxSize lineBreakMode:NSLineBreakByWordWrapping];
        //this will return correct height for text
        return cellSize.height+100;
   }
}


 -(void)reply_comment:(UIButton*)sender
 {
      buttontag1=sender.tag;
      NSLog(@"I Clicked a button  button tag %d",buttontag1);
      NSString *child=[replyArray objectAtIndex:sender.tag];
      if(child== nil || child == (id)[NSNull null]||[child isEqualToString:@"0"])
      {
           NSLog(@"no child post");
      }
      else
      {        
          NSLog(@" child post");
         ChildViewController *objChildViewController=[[ChildViewController              alloc]initWithNibName:@"ChildViewController" bundle:nil];
         [cell.child setHidden:NO];
         [objChildViewController getchild:posts_id:buttontag1 :uid:frame2];
         [cell.child addSubview:objChildViewController.view];                   
       }
   }

请有人帮帮我吗?

1 个答案:

答案 0 :(得分:0)

希望这会有所帮助。

@interface ViewController ()
{
int currentselection;
int cellno;
UITableView *table;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// do things with your cell here

// set selection
cellno = indexPath.row;


}
-(void)buttonpressed
{
currentselection = cellno;
[table beginUpdates];
[table endUpdates];

}
-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
int rowHeight;
if ([indexPath row] == currentselection) {
    rowHeight = 200;
} else rowHeight = 100;
return rowHeight;
}