UITableViewCell with dynamic height iOS

前端 未结 12 2286
日久生厌
日久生厌 2020-11-27 17:54

I have implemented TableView with CustomCell in my app,

I want dynamic height of my UITableViewCell according to text length in UITableViewCell

相关标签:
12条回答
  • 2020-11-27 18:51

    The following code worked fine for me.Try with this

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        CGFloat lRetval = 10;
        CGSize maximumLabelSize = CGSizeMake(231, FLT_MAX);
        CGSize expectedLabelSize;
    
    
        CGFloat numberoflines = [thirdcellText length]/17.0;
    
        if (indexPath.section == 0) {
            expectedLabelSize = [firstcellText sizeWithFont:[UIFont systemFontOfSize:16.0]
                                          constrainedToSize:maximumLabelSize
                                              lineBreakMode:NSLineBreakByWordWrapping];
            lRetval = expectedLabelSize.height;
        }
        else if(indexPath.section == 1)
        {
            expectedLabelSize = [secondcellText sizeWithFont:[UIFont systemFontOfSize:16.0]
                                           constrainedToSize:maximumLabelSize
                                               lineBreakMode:NSLineBreakByWordWrapping];
            lRetval = expectedLabelSize.height;
        }
        else if (indexPath.section == 2)
        {
            expectedLabelSize = [thirdcellText sizeWithFont:[UIFont systemFontOfSize:16.0]
                                           constrainedToSize:CGSizeMake(231, numberoflines*17.0)
                                               lineBreakMode:NSLineBreakByWordWrapping];
            lRetval = expectedLabelSize.height-128.0;
        }
    
        UIImage *factoryImage = [UIImage imageNamed:NSLocalizedString(@"barcode_factory_reset.png", @"")];
    
        CGFloat height = factoryImage.size.height;
    
        if (lRetval < height) {
            lRetval = height+15.0;
        }
    
        return lRetval;
    }
    

    Try to add the following code in your customcell class autolayout method

    textview.frame = frame;
    CGRect frame1 = textview.frame;
    frame1.size.height = textview.contentSize.height-2;
    textview.frame = frame1;
    
    
    textview.contentSize = CGSizeMake(textview.frame.size.width, textview.frame.size.height);
    
    labelPtr.frame = CGRectMake(CGRectGetMinX(imageView.frame)+CGRectGetMaxX(imageView.frame)+5.0, textview.frame.size.height+10.0, 140, 16.0);
    [labelPtr setNeedsDisplayInRect:labelPtr.frame];
    

    Try to set the label properties like the following

    labelPtr = [[UILabel alloc] initWithFrame:CGRectZero];
    labelPtr.backgroundColor =[UIColor clearColor];
    [labelPtr setNeedsLayout];
    [labelPtr setNeedsDisplay];
    [self.contentView addSubview:labelPtr];
    
    0 讨论(0)
  • 2020-11-27 18:53

    I saw a lot of solutions but all was wrong or uncomplet. You can solve all problems with 5 lines in viewDidLoad and autolayout. This for objetive C:

    _tableView.delegate = self;
    _tableView.dataSource = self;
    self.tableView.estimatedRowHeight = 80;//the estimatedRowHeight but if is more this autoincremented with autolayout
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    [self.tableView setNeedsLayout];
    [self.tableView layoutIfNeeded];
    self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0) ;
    

    For swift 2.0:

     self.tableView.estimatedRowHeight = 80
     self.tableView.rowHeight = UITableViewAutomaticDimension      
     self.tableView.setNeedsLayout()
     self.tableView.layoutIfNeeded()
     self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0)
    

    Now create your cell with xib or into tableview in your Storyboard With this you no need implement nothing more or override. (Don forget number os lines 0) and the bottom label (constrain) downgrade "Content Hugging Priority -- Vertical to 250"

    You can donwload the code in the next url: https://github.com/jposes22/exampleTableCellCustomHeight

    References: http://candycode.io/automatically-resizing-uitableviewcells-with-dynamic-text-height-using-auto-layout/

    0 讨论(0)
  • 2020-11-27 18:53

    I just wrote about this problem and the approach I finally chose. You can read about it here: Dynamic UITableView Cell Height Based on Contents

    Basically, I created a UITableView subclass that handles all this for both default and custom cells. It probably needs tweaking, but I have used it as is with good result.

    You can grab the code here: https://github.com/danielsaidi/DSTableViewWithDynamicHeight

    Hope it helps (...and if it didn't, I apologize and would love to hear why not)

    0 讨论(0)
  • 2020-11-27 18:56

    Could you try this;

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
        {
         int topPadding = cell.yourLabel.frame.origin.x;
         int bottomPadding = cell.frame.size.heigth-(topPadding+cell.yourLabel.frame.size.height);
         NSString *text = [DescArr objectAtIndex:[indexPath row]];
         CGSize maximumSize = CGSizeMake(cell.yourLabel.frame.size.width, 9999);
         CGSize expectedSize = [text sizeWithFont:yourCell.yourLabel.font constrainedToSize:maximumSize lineBreakMode:yourCell.yourLabel.lineBreakMode];
    
         return topPadding+expectedSize.height+bottomPadding;
    }
    
    0 讨论(0)
  • 2020-11-27 18:59

    This is very simple now
    Use below steps

    1. Set constraint to your Label (If using custom cell)
    2. Number of line must be 0
    3. Setup few properties of UITableView

    self.tableView.estimatedRowHeight = 100.0;
    self.tableView.rowHeight = UITableViewAutomaticDimension;

    Enjoy:)
    For more detail you can check
    www.raywenderlich.com
    stackoverflow.com

    0 讨论(0)
  • 2020-11-27 18:59

    I needed a dynamic table view cell height based on the amount of text to be displayed in that cell. I solved it in this way:

        - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
        {
            if (!isLoading)
            {
    
                if ([self.conditionsDataArray count]>0)
                {
                    Conditions *condition =[self.conditionsDataArray objectAtIndex:indexPath.row];
                    int height;
                    UITextView *textview = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 236, 0)];   //you can set your frame according to your need
                    textview.text  = condition.comment;
                    textview.autoresizingMask = UIViewAutoresizingFlexibleHeight;
                    [tableView addSubview:textview];
                    textview.hidden = YES;
                    height = textview.contentSize.height;
                    NSLog(@"TEXT VIEW HEIGHT %f", textview.contentSize.height);
                    [textview removeFromSuperview];
                    [textview release];
                    return height;
           }
           return 55;  //Default height, if data is in loading state
    }
    

    Notice that the Text View has been added as Subview and then made hidden, so make sure you add it as SubView otherwise it's height will not be considered.

    0 讨论(0)
提交回复
热议问题