简体   繁体   中英

how to dynamically change the height of tableview cell for custom cell?

I have created a custom cell table view.This custom cell contains labels so i have added labeled from storyboard & i show data on them from server.So if there is no data for the label then i hide the label but if there is data then i show the label with server data.So teh height of cell remain lets say 180 if there is data on label or not because label remain always there.Now when there is no data for the label then i want to remove or hide label & decrease the height of cell because all labels shown vertically.In this way i wan to calculate the height of cell.

Problems with HeightForAtIndex

  • How to access cell in heightForRow?
  • What code to write i get the cell in heightForRowAtIndex?

EDIT:

I have tried this code:

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

      float like_height,c_one_height,c_two_height,c_three_height,tv_height,header_height,operations_height;
    //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];
    [self.customCell setSelectionStyle:UITableViewCellSelectionStyleNone];

    if(like_count>0)
    {
        like_height=self.customCell.label_like_count.frame.size.height;

    }
    else
    {
        like_height=0;
    }
    if(comment_count<=0)
    {
        c_one_height=0;
        c_two_height=0;
        c_three_height=0;

    }
    else if(comment_count==1)
    {
        c_one_height=self.customCell.first_comment.frame.size.height;
        c_two_height=0;
        c_three_height=0;

    }
    else if(comment_count==2)
    {
        c_one_height=self.customCell.first_comment.frame.size.height;
        c_two_height=self.customCell.second_cmment.frame.size.height;
        c_three_height=0;
    }
    else if(comment_count==3)
    {
        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;
    }
    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;
    CGFloat height = like_height+c_one_height+c_two_height+c_three_height+tv_height+header_height+operations_height;
    // Padding of 1 point (cell separator)
    CGFloat separatorHeight = 1;
    return height + separatorHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *text=[InformationArray objectAtIndex:indexPath.row];
    UIFont *font = [UIFont fontWithName:@"HelveticaNeue" size:16.5];
     text = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    CGSize labelHeight = [self heigtForCellwithString:text  withFont:font];

 return labelHeight.height +10;

}
//calculate the height
-(CGSize)heigtForCellwithString:(NSString *)stringValue withFont:(UIFont*)font{

CGSize constraint = CGSizeMake(250,9999); 
    NSDictionary *attributes = @{NSFontAttributeName: font};
    CGRect rect = [stringValue boundingRectWithSize:constraint options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:attributes context:nil];

    return rect.size;

}

If you are using the autolayout then to resize table cell or any view, better way is to update the NSLayoutConstraint of view.

To update the Table view cell, please refer the sample tutorial : http://derpturkey.com/autosize-uitableviewcell-height-programmatically/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM