增加细胞高度而不配置它?

时间:2015-10-28 06:08:48

标签: ios objective-c iphone uitableview ipad

我有UITableView因为我有自定义单元格。自定义单元格包含一些标签。标签文本可以是动态的,具体取决于Json。现在我想根据标签的内容高度来制作单元格的高度,所以我使用了方法heightForRowAtINdexPath。现在我得到了这样的单元格。

if(!self.customCell)
    {
        self.customCell = [self.table_view dequeueReusableCellWithIdentifier:@"homeCell"];
    }

标签可以是多行的,具体取决于数据。如果我在cellForRowAtiNdexPath中编写heightForRowAtIndexPath的相同代码,那么我会获得更多高度。但如果我不写相同的代码然后我的标签高度减少了。 截至目前,我已经为heightForRowAtIndexPath编写了以下代码。

-   (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height;
      float like_height,c_one_height,c_two_height,c_three_height,tv_height,header_height,operations_height,img_like_icon_height,img_comment_icon_height,header_margin=5,tv_container_margin=5,operations_margin=5,like_top_margin=11,first_comment=10,second_comment=5,third_comment=5;
    //define variables here
    if(!self.customCell)
    {
        self.customCell = [self.table_view dequeueReusableCellWithIdentifier:@"homeCell"];
    }
    Post *user_post=[arr_post objectAtIndex:indexPath.row];
    int like_count=[user_post.like_count intValue];
    float comment_count=[user_post.comment_count intValue];
    if (self.customCell.beizer_image != nil)

    {
        NSLog(@"inside cell beizer");

        [self.customCell.beizer_image removeFromSuperview];

        self.customCell.beizer_image=nil;

        self.customCell.tv_post.textContainer.exclusionPaths=NULL;

        self.customCell.beizer_path=nil;

    }
    [self.customCell setSelectionStyle:UITableViewCellSelectionStyleNone];
    self.customCell.tv_post.text=user_post.post_description;
    self.customCell.tv_post.font = [UIFont fontWithName:user_post.font_family size:[user_post.font_size floatValue]];
    [self.customCell.tv_post setTextColor:[self colorFromHexString:user_post.font_color]];
    if([user_post.post_image isEqualToString:@"none"] && [user_post.post_video isEqualToString:@"none"])
    {

        NSLog(@"NOT INSIDE THE CONDITION");

    }
    else
    {
        NSLog(@"INSIDE BEIZER PATH CONDITION");
        self.customCell.beizer_path = [UIBezierPath bezierPathWithRect:CGRectMake(5, 5, 100, 100)];
        self.customCell.tv_post.textContainer.exclusionPaths = @[self.customCell.beizer_path];
        self.customCell.beizer_image =[[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 100, 100)];

        if(![user_post.post_image isEqualToString:@"none"])
        {
            [self.customCell.beizer_image setImageWithURL:[NSURL URLWithString:[IMAGE_BASE_URL stringByAppendingString:user_post.post_image]]placeholderImage:[UIImage imageNamed:@"post_placeholder.png"]];

        }
        if(![user_post.post_video isEqualToString:@"none"])
        {
            [self.customCell.beizer_image setImageWithURL:[NSURL URLWithString:[IMAGE_BASE_URL stringByAppendingString:user_post.post_video_thumbnail]]placeholderImage:[UIImage imageNamed:@"post_placeholder.png"]];

        }
        [self.customCell.tv_post addSubview:self.customCell.beizer_image];
    }
    //make textview height dynamic
    self.customCell.tv_post.scrollEnabled=NO;
    if([user_post.post_image isEqualToString:@"none"] && [user_post.post_video isEqualToString:@"none"])
    {
        CGFloat fixedWidth = self.customCell.tv_post.frame.size.width;
        CGSize newSize = [self.customCell.tv_post sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
        CGRect newFrame = self.customCell.tv_post.frame;
        newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
        self.customCell.tv_post.frame = newFrame;
        self.customCell.tv_height.constant=self.customCell.tv_post.frame.size.height;
        [self.customCell.view_tvContainer layoutIfNeeded];
    }
    else
    {
        CGFloat fixedWidth = self.customCell.tv_post.frame.size.width;
        CGSize newSize = [self.customCell.tv_post sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
        CGRect newFrame = self.customCell.tv_post.frame;
        newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth),fmax(self.customCell.beizer_image.frame.size.height+10,newSize.height));
        self.customCell.tv_post.frame = newFrame;
        self.customCell.tv_height.constant=self.customCell.tv_post.frame.size.height;
        [self.customCell.view_tvContainer layoutIfNeeded];
    }

//    self.customCell.f_comment_top.constant=-2100;
//    [self.customCell layoutIfNeeded];
    if(like_count>0)
    {
        like_height=self.customCell.label_like_count.frame.size.height;
    }
    else
    {
        like_height=0;
        img_like_icon_height=0;
    }
    if(comment_count<=0)
    {
        c_one_height=0;
        c_two_height=0;
        c_three_height=0;
        img_comment_icon_height=0;
    }
    else if(comment_count==1)
    {

        if(like_count<=0)
        {
            self.view.translatesAutoresizingMaskIntoConstraints = NO;
            self.customCell.f_comment_top.constant=-21;
            [self.customCell layoutIfNeeded];
        }
        c_one_height=self.customCell.first_comment.frame.size.height;
        c_two_height=0;
        c_three_height=0;

    }
    else if(comment_count==2)
    {
        if(like_count<=0)
        {
            self.view.translatesAutoresizingMaskIntoConstraints = NO;
            self.customCell.f_comment_top.constant=-21;
            [self.customCell layoutIfNeeded];
        }
        c_one_height=self.customCell.first_comment.frame.size.height;
        c_two_height=self.customCell.second_cmment.frame.size.height+15;
        c_three_height=0;
    }
    else if(comment_count==3)
    {
        if(like_count<=0)
        {
//            self.customCell.f_comment_top.constant=1020;
//            [self.customCell.first_comment layoutIfNeeded];
        }
        else
        {

        }
        c_one_height=self.customCell.first_comment.frame.size.height;
        c_two_height=self.customCell.second_cmment.frame.size.height;
        c_three_height=self.customCell.third_comment.frame.size.height+15;
    }
    tv_height=self.customCell.view_tvContainer.frame.size.height;
    header_height=self.customCell.header_view_height.frame.size.height;
    operations_height=self.customCell.view_operations_height.frame.size.height;
    height = like_height+c_one_height+c_two_height+c_three_height+tv_height+operations_height+header_height;
    CGFloat margin_height=header_margin+operations_margin+tv_container_margin+like_top_margin+first_comment+second_comment+third_comment+img_comment_icon_height+img_like_icon_height;
    NSLog(@"like label height is %f",like_height);
    NSLog(@"first comment height is %f",c_one_height);
    NSLog(@"second comment height is %f",c_two_height);
    NSLog(@"third comment height is %f",c_three_height);
    NSLog(@"all margin  height is %f",margin_height);
    NSLog(@"height is %f",height);

    // Padding of 1 point (cell separator)
    height=height+margin_height;
    CGFloat separatorHeight = 1;
    return height;

}

我已经看到很多例子,其中写道,为了配置单元格,cellForRowAtIndexPath的代码写在heightForRowAtIndexPath中。

  1. 为什么我需要重复cellForRowAtIndexPath的代码 heightForRowAtIndexPath

2 个答案:

答案 0 :(得分:0)

为什么我需要在heightForRowAtIndexPath中重复cellForRowAtIndexPath的代码?

  
    

因为heightForRowAtIndexPath将始终在cellForRowAtIndexPath之前被调用。因此,要计算heightForRowAtIndexPath中的高度,您需要在heightForRowAtIndexPath中创建该单元格。

  

使用autolayout进行动态高度计算的最佳方法: - 您可以参考以下Link来执行相同操作。

答案 1 :(得分:0)

您无需在entirecellForRowAtIndexPath中配置已声明的heightForRowAtIndexPath单元格。

这是用于特定功能的两种方法。

cellForRowAtIndexPath是一种DataSource方法,用于在tableview中配置您的单元格。

heightForRowAtIndexPath是一种Delegate方法,用于根据您计划在{{1}中显示的内容计算tableviewcellrow的高度}}

例如,如果您使用cellForRowAtIndexPath配置单元格(无论标签数量多少)并在UILabel中为标签指定一些文字,则在cellForRowAtIndexPath中,获取已分配给标签的文本,计算标签的高度并将标签的高度赋予单元格。这将创建根据标签高度高度的单元格。

例如,在heightForRowAtIndexPath

cellForRowAtIndexPath
.....//your cell configuration code above//..... UILabel *lbl_myText = [[UILabel alloc]initWithFrame:CGRectZero]; [lbl_myText setLineBreakMode:NSLineBreakByWordWrapping]; lbl_myText.minimumScaleFactor = FONT_SIZE; [lbl_myText setNumberOfLines:0]; lbl_myText.textAlignment = NSTextAlignmentRight; [lbl_myText setFont:[UIFont systemFontOfSize:FONT_SIZE]]; NSString *text = [arr_text objectAtIndex:indexPath.row]; CGSize constraint = CGSizeMake(Your_MAX_LabelWidth, Your_MAX_LabelHeight); NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping; CGRect textRect = [text boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:FONT_SIZE], NSParagraphStyleAttributeName: paragraphStyle.copy} context:nil]; size = textRect.size; [lbl_myText setText:text]; float widthIs = [lbl_myText.text boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName:lbl_myText.font } context:nil] .size.width; [lbl_myText setFrame:CGRectMake(X_co,Y_co,widthIs,size.height)]; 中的

heightForRowAtIndexPath