2011-10-09 70 views
0

我有视图控制器,其中包含帖子详细信息&表视图。表格单元格是自定义的。我已经在单元格上放置了uitebel,uibutton & uitextview。此表单元格包含该帖子的回复&的回复。所以表格单元高度是动态的。如果评论有答复,则正确显示单元格的高度。但如果评论没有回复则表格单元格高度计算错误。我想要表格单元&表格高度根据单元格的内容应该是动态的。我的视图控制器conatins另一个视图被称为“内容视图”,我已经放置了我的表视图的帖子细节&。以下是我的形象,可以让你了解我的观点。 下面的图片仅用于获取观点。使用xcode 4的iPhone动态表格视图单元格高度

enter image description here

这里是我的代码

#define TABLE_CELL_HEIGHT 200.0f 
#define ADD_VIEW_HEIGHT 30.0f 
#define FONT_SIZE 24.0f 
#define CELL_CONTENT_WIDTH 720.0f 
#define CELL_CONTENT_MARGIN 50.0f 
#define SectionHeight 50.0f 
#define LEFT_MARGIN 25 
#define RIGHT_MARGIN 25 
#define TOP_MARGIN 35 
#define BOTTOM_MARGIN 50 
#define BOTTOM_FOOTER_MARGIN 32 
#define DOC_WIDTH 768 
#define DOC_HEIGHT 910 

- (void)viewDidLoad 
{ 

    [super viewDidLoad]; 
    [self createUserInterface]; 
    [self createCommentUserInterface]; 

    self.contentView.backgroundColor = SET_BACKGROUND_IMAGE; 
    self.view.backgroundColor = SET_BACKGROUND_IMAGE; 
} 



- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

    [self displayCommentsForPhotoID:[post ID]]; 
    [self.commentsTableView reloadData]; 

    CGRect frame = self.commentsTableView.frame; 
    frame.size.height = TABLE_CELL_HEIGHT*[commentsArray count]; 
    self.commentsTableView.frame = frame; 

    CGRect viewFrame = self.contentView.frame; 
    viewFrame.size.height = viewFrame.size.height + (TABLE_CELL_HEIGHT*[commentsArray count]) + ADD_VIEW_HEIGHT; 
    self.contentView.frame = viewFrame; 

    [self.scrollView addSubview:self.contentView]; 
    self.scrollView.contentSize = self.contentView.bounds.size; 
} 


    -(void)createCommentUserInterface { 

    UILabel *lblComments = [[UILabel alloc]initWithFrame:CGRectMake(20.0f, 570.0f, 150.0f, 30.0f)]; 
    [lblComments setBackgroundColor:[UIColor clearColor]]; 
    [lblComments setText:@"Comments"]; 
    [self.contentView addSubview:lblComments]; 

    UIButton *btnAddComment = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [btnAddComment addTarget:self 
         action:@selector(btnAddCommentPressed) 
      forControlEvents:UIControlEventTouchDown]; 

    [btnAddComment setBackgroundImage:[UIImage imageNamed:@"btn_addcomment.png"] forState:UIControlStateNormal]; 
    btnAddComment.frame = CGRectMake(20.0f, 610.0f, 140.0f, 40.0f); 
    [self.contentView addSubview:btnAddComment]; 

    commentsTableView = [[UITableView alloc]initWithFrame:CGRectMake(20.0f, 660.0f, 280.0f, 600.0f) style:UITableViewStylePlain]; 
    commentsTableView.delegate = self; 
    commentsTableView.dataSource = self; 

    [self.contentView addSubview:commentsTableView]; 

} 



    #pragma mark - Table view data source 

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [self.commentsArray count]; 
} 



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell; 

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    cell = nil; 


    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    [tableView setSeparatorColor:[UIColor darkGrayColor]]; 
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
    cell.backgroundColor = [UIColor whiteColor]; 
    userComment = [commentsArray objectAtIndex:[indexPath row]]; 

    UILabel *lblSubmittedBy = [[UILabel alloc]initWithFrame:CGRectMake(10.0f, 5.0f, 100.0f, 30.0f)]; 
    [lblSubmittedBy setText:@"Submitted by"]; 
    [cell addSubview:lblSubmittedBy]; 

    UIButton *btnUserName = [UIButton buttonWithType:UIButtonTypeCustom]; 
    btnUserName.frame = CGRectMake(120.0f, 5.0f, 150.0f, 30.0f); 
    [btnUserName setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft]; 
    [btnUserName setBackgroundColor:[UIColor clearColor]]; 
    [btnUserName setTitleColor:[UIColor cyanColor] forState:UIControlStateNormal]; 
    [btnUserName addTarget:self action:@selector(showUserProfileForUserComment:) forControlEvents:UIControlEventTouchUpInside]; 
    [btnUserName setUserInteractionEnabled:YES]; 
    [cell addSubview:btnUserName]; 

    UILabel *lblOnDate = [[UILabel alloc]initWithFrame:CGRectMake(10.0f, 40.0f, 20.0f, 30.0f)]; 
    [lblOnDate setText:@"on"]; 
    [cell addSubview:lblOnDate]; 

    UILabel *lblDate = [[UILabel alloc]initWithFrame:CGRectMake(35.0f, 40.0f, 200.0f, 30.0f)]; 
    [cell addSubview:lblDate]; 

    UITextView *txtVwComments = [[UITextView alloc]initWithFrame:CGRectMake(5.0f, 75.0f, 265.0f, 70.0f)]; 
    txtVwComments.backgroundColor = cell.backgroundColor; 
    [txtVwComments setFont:[UIFont fontWithName:@"Helvetica" size:17.0f]]; 
    [txtVwComments setEditable:NO]; 
    [cell addSubview:txtVwComments]; 

    [txtVwComments setText:[userComment Comment]]; 

    CGRect frame = txtVwComments.frame; 
    frame.size.height = txtVwComments.contentSize.height; 
    CGFloat height = frame.size.height; 

    NSLog(@"Value of height => %f",height); 
    txtVwComments.frame = frame; 

    CGFloat ht1 = height + 20.0f + 75.0f; 
    UIButton *btnUpVote = [[UIButton alloc]initWithFrame:CGRectMake(10.0f, ht1 + 10.0f, 16.0f, 16.0f)]; 
    [btnUpVote setBackgroundColor:[UIColor clearColor]]; 
    [btnUpVote setBackgroundImage:[UIImage imageNamed:@"thumb_up.png"] forState:UIControlStateNormal]; 
    [btnUpVote addTarget:self 
         action:@selector(btnUpVotePressed:) 
      forControlEvents:UIControlEventTouchDown]; 
    [btnUpVote setTag:[userComment ID]]; 
    [cell addSubview:btnUpVote]; 
    NSLog(@"Value of ht1 => %f",ht1); 

    lblUpVoteCount = [[UILabel alloc]initWithFrame:CGRectMake(36.0f, ht1 , 30.0f, 30.0f)]; 
    [lblUpVoteCount setTag:[userComment ID]]; 
    [cell addSubview:lblUpVoteCount]; 

    UIButton *btnDownVote = [[UIButton alloc]initWithFrame:CGRectMake(90.0f, ht1 + 10.0f , 16.0f, 16.0f)]; 
    [btnDownVote setBackgroundColor:[UIColor clearColor]]; 
    [btnDownVote setBackgroundImage:[UIImage imageNamed:@"thumb_down.png"] forState:UIControlStateNormal]; 
    [btnDownVote addTarget:self 
        action:@selector(btnDownVotePressed:) 
      forControlEvents:UIControlEventTouchDown]; 
    [btnDownVote setTag:[userComment ID]]; 
    [cell addSubview:btnDownVote]; 

    lblDownVoteCount = [[UILabel alloc]initWithFrame:CGRectMake(116.0f, ht1 , 40.0f, 30.0f)]; 
    [lblDownVoteCount setTag:[userComment ID]]; 
    [cell addSubview:lblDownVoteCount]; 

    UIButton *btnReply = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    btnReply.frame = CGRectMake(180.0f, ht1, 60.0f, 30.0f); 
    [btnReply setBackgroundColor:[UIColor clearColor]]; 
    [btnReply setBackgroundImage:[UIImage imageNamed:@"btn_reply.png"] forState:UIControlStateNormal]; 
    [btnReply addTarget:self action:@selector(btnReplyPressed:) forControlEvents:UIControlEventTouchDown]; 
    [btnReply setTag:[userComment ID]]; 
    [cell addSubview:btnReply]; 

    float y = ht1 + 40.0f; 
    for (int i=0; i < [userComment.replyArray count]; i++) { 

     if ([userComment.replyArray objectAtIndex:i] != nil) { 

      UITextView *txtVwReply = [[UITextView alloc]initWithFrame:CGRectMake(40.0f, y , 220.0,60.0f)]; 
      [txtVwReply setUserInteractionEnabled:NO]; 
      [txtVwReply setFont:[UIFont fontWithName:@"Helvetica" size:14.0f]]; 
      [cell addSubview:txtVwReply]; 

     commentReply = [userComment.replyArray objectAtIndex:i]; 

      NSLog(@"userComment.replyArray => %@",userComment.replyArray); 
      NSLog(@"commReply object => %@",commentReply); 

      strReply = [NSString stringWithFormat:@"Submitted by %@ \n on %@\n %@",[commentReply ReplyUsername],[commentReply ReplyDateAdded],[commentReply ReplyComment]]; 
      NSLog(@"strReply => %@",strReply); 
      [txtVwReply setText:strReply]; 
      [txtVwReply release]; 
      y = y +80; 
     } 
    } 

    [btnUserName setTitle:[userComment Username] forState:UIControlStateNormal]; 

    [lblDate setText:[userComment DateAdded]]; 
    [lblUpVoteCount setText:[NSString stringWithFormat:@"%i",[userComment UpVotes]]]; 
    [lblDownVoteCount setText:[NSString stringWithFormat:@"%i",[userComment DownVotes]]]; 

    [lblSubmittedBy release]; 
    [lblOnDate release]; 
    [lblDate release]; 
    [txtVwComments release]; 
    [btnUpVote release]; 
    [lblUpVoteCount release]; 
    [btnDownVote release]; 
    [lblDownVoteCount release]; 

    return cell; 
} 

#pragma mark - Table view delegate 

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // I don't want this event 
} 

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

    if ([commentsArray count]>0) { 


     userComment = [commentsArray objectAtIndex:indexPath.row]; 
     NSString *strComment =userComment.Comment; 
     NSLog(@"strComment : - %@",strComment); 


     CGFloat replyHeight = 50.0f; 
     if([userComment.replyArray count]>0) { 
      for (int i=0; i < [userComment.replyArray count]; i++) { 
       commentReply = [userComment.replyArray objectAtIndex:i]; 
       NSString *strCommentReply = commentReply.ReplyComment; 
       NSLog(@"strCommentReply => %@",strCommentReply); 

       [strCommentReply stringByAppendingFormat:@"\n %@",strCommentReply]; 
       replyHeight = replyHeight + 50; 
      } 
     } 

     CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f); 

     CGSize sizeComment = [strComment sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; 

     CGFloat height = MAX(sizeComment.height + replyHeight, 44.0f); 

     NSLog(@"height %f",height); 

     NSLog(@"height + (CELL_CONTENT_MARGIN) + TABLE_CELL_HEIGHT => %f",height + (CELL_CONTENT_MARGIN) + TABLE_CELL_HEIGHT); 
     NSLog(@"TABLE_CELL_HEIGHT - %f",height + (CELL_CONTENT_MARGIN) + TABLE_CELL_HEIGHT); 

     return height + (CELL_CONTENT_MARGIN) + TABLE_CELL_HEIGHT; 


    } 

    NSLog(@"TABLE_CELL_HEIGHT - %f",TABLE_CELL_HEIGHT); 

    return TABLE_CELL_HEIGHT; 
} 

请指导我这个问题。由于

代码更新

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

// Code for placing UI components on the cell .Same as above code 

    float y = ht1 + 40.0f; 

    for (int i=0; i < [userComment.replyArray count]; i++) { 

     if ([userComment.replyArray objectAtIndex:i] != nil) { 

      UITextView *txtVwReply = [[UITextView alloc]initWithFrame:CGRectMake(40.0f, y , 220.0,0.0f)]; 
      [txtVwReply setUserInteractionEnabled:NO]; 
      [txtVwReply setFont:[UIFont fontWithName:@"Helvetica" size:14.0f]]; 
      [cell addSubview:txtVwReply]; 

      commentReply = [userComment.replyArray objectAtIndex:i]; 

      NSLog(@"userComment.replyArray => %@",userComment.replyArray); 
      NSLog(@"commReply object => %@",commentReply); 

      strReply = [NSString stringWithFormat:@"Submitted by %@ \n on %@\n %@",[commentReply ReplyUsername],[commentReply ReplyDateAdded],[commentReply ReplyComment]]; 
      NSLog(@"strReply => %@",strReply); 

      [txtVwReply setText:strReply]; 

      CGRect frame = txtVwReply.frame; 
      frame.size.height = txtVwReply.contentSize.height; 
      CGFloat height = frame.size.height; 

      NSLog(@"Value of height => %f",height); 
      txtVwReply.frame = frame; 

      y = y + 80; 

      [txtVwReply release]; 

     } 
    } 
} 


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

    if ([commentsArray count]>0) { 

     userComment = [commentsArray objectAtIndex:indexPath.row]; 
     NSString *strComment =userComment.Comment; 
     NSLog(@"strComment : - %@",strComment); 

     CGFloat replyHeight = 135.0f; 
     if([userComment.replyArray count]>0) { 
      for (int i=0; i < [userComment.replyArray count]; i++) { 
       commentReply = [userComment.replyArray objectAtIndex:i]; 
       NSString *strCommentReply = commentReply.ReplyComment; 
       NSLog(@"strCommentReply => %@",strCommentReply); 
       // Append all the reply & then use to determine hight 
       [strCommentReply stringByAppendingFormat:@"\n %@",strCommentReply]; 
       replyHeight = replyHeight + 100.0f; 
      } 
     } 

     CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f); 

     CGSize sizeComment = [strComment sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; 

     CGFloat height = MAX(sizeComment.height + replyHeight + 40.0f, 44.0f); 

     NSLog(@"height %f",height); 

     NSLog(@"height + (CELL_CONTENT_MARGIN) + TABLE_CELL_HEIGHT => %f",height + (CELL_CONTENT_MARGIN) + TABLE_CELL_HEIGHT); 
     NSLog(@"TABLE_CELL_HEIGHT - %f",height + (CELL_CONTENT_MARGIN) + TABLE_CELL_HEIGHT); 


     return height; 
     } 

    NSLog(@"TABLE_CELL_HEIGHT - %f",TABLE_CELL_HEIGHT); 

    return TABLE_CELL_HEIGHT; 
} 
+0

当你说这是错误地计算出的负载细胞,你是什么意思?太小,太大? – jrturton

+0

这些NSLog行的输出是什么。你是否从heightForRowAtIndexPath返回预期的数字? –

回答

2

我不明白,你已经实现了的tableview委托方法:

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

这正是我一定能够来测试你的内容,并提供细胞的大小。

+0

向下滚动 - 实施 – Colin

+0

我的不好,我现在看到它。 –

+0

下来...下来... – jrturton

1

它在我看来你的计算是不正确的。如果我正确地遵循它,我猜测只有几个答复它只会出现接近正确的大小。太多评论,它会太小,没有评论你的细胞高度会过大。 这是因为你基本上是计算你的整体行高为:

50.0(为replyHeight初始值)+ commentHeight +(50.0 X replyArray.count)

,这将是50您的意见,如果+高度没有回复(注意永远不会少于44,所以你的MAX永远不会少回)。然后,您可以将常量TCM和CCH添加到总计300+的注释高度。

在您的cellForRowAtIndexPath中,您似乎只需要评论的高度仅为135.0。所以如果你没有答复,你会有一个几乎两倍的大小所需的行。 只有少数回复才会显示正确的原因是,您对每个回复使用80.0,但只是将50.0添加到行高。它每次接近30点,然后在大约4或5次迭代中会平均。任何更多,它会开始太小。

我认为如果你初始化replyHeight为135.0,并将replyHeight的增量改为80而不是50.0,那么计算应该更接近。在这个浏览器中很难遵守,我可能会失去一些。 然后从返回值中删除两个变量TABLE_CONTENT_MARGIN和TABLE_CELL_HEIGHT。 135 + commentHeight +(80 x replyArray.count)的返回值应该是正确的。你不需要遍历注释,因为你没有使用任何我能看到的字符串。你可能也需要这个T_C_M,但是当你使用它的时候你应该看到这一点很简单。

+0

感谢您的回复。但有时它并不像预期那样工作。如果我第一次添加回复以进行评论,则它不在单元格内。它在单元格外(大评论)。如果我添加另一个回复,那么第一个回复将被裁剪意味着它显示部分回复,但第二个回复显示正确。我已经更新了代码。查看我的更新。谢谢 – iOSAppDev

0

temparray是数组您用于的cellForRowAtIndexPath

(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *str = [temparray objectAtIndex:indexPath.row];  
    If (str. length > 50) { 
     return 85; 
    } 
    else { 
     return 60; 
    } 
} 
相关问题