Increase the main tableview row height according to the custom cell

前端 未结 7 695
名媛妹妹
名媛妹妹 2021-01-03 07:32

I am having an app in which I have a Tableview and on that tableview\'s each row I am dynamically creating a custom tableview cell.

Below is the cod

7条回答
  •  死守一世寂寞
    2021-01-03 07:55

    You can use the following code to adjust the height dynamically. First you need to determine the height of the label and then adjust the height of the cell accordingly. I have been using this code in my chat application and works fine.

    First, create the label and image view in cellForRowAtIndexPath:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
                /// Set Text Label
    
                UILabel *lbl_myText = [[UILabel alloc]initWithFrame:CGRectZero];
                [lbl_myText setLineBreakMode:NSLineBreakByWordWrapping];
                lbl_myText.minimumScaleFactor = FONT_SIZE;
                [lbl_myText setNumberOfLines:0];
                lbl_myText.textAlignment = NSTextAlignmentLeft;
                [lbl_myText setFont:[UIFont systemFontOfSize:FONT_SIZE]];
    
                NSString *text = [arr_text objectAtIndex:indexPath.row];
    
                CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE]];
    
                // Checks if text is multi-line
                if (size.width > lbl_myText.bounds.size.width)
                {
                    CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f); //// Here Width = Width you want to define for the label in its frame. The height of the label will be adjusted according to this.
    
                    //CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
    
                    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];
    
                    CGSize size = textRect.size;
    
                    [lbl_myText setText:text];
                    [lbl_myText setFrame:CGRectMake(cell.imgv_someoneImage.frame.size.width+8, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - cell.imgv_someoneImage.frame.size.width -(CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];
                }
    
                else
                {
                    lbl_myText.frame = CGRectMake(10, 0, cell.frame.size.width - cell.imgv_someoneImage.frame.size.width - 18,18);
                    lbl_myText.textAlignment = NSTextAlignmentLeft;
                    [lbl_myText setText:text];
                }
    
                //lbl_myText.backgroundColor = [UIColor greenColor];
    
                [cell.contentView addSubview:lbl_myText];
    
                /// Set Date Label
    
                NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
                [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
                NSString *stringFromDate = [formatter stringFromDate:[arr_date objectAtIndex:indexPath.row]];
    
                UILabel *lbl_myDate = [[UILabel alloc]initWithFrame:CGRectMake(cell.imgv_someoneImage.frame.size.width+8, lbl_myText.frame.size.height+10, cell.frame.size.width - cell.imgv_someoneImage.frame.size.width - 10 ,18)];
                lbl_myDate.text = stringFromDate;
                lbl_myDate.font = [UIFont fontWithName:@"Helvetica Neue" size:13.0];
                lbl_myDate.textColor = [UIColor lightGrayColor];
                lbl_myDate.textAlignment = NSTextAlignmentLeft;
                [cell.contentView addSubview:lbl_myDate];
    
                /// Set User Image
    
                UIImageView *imgv_myImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, lbl_myText.frame.origin.y, 63, 63)];
                imgv_myImage.image = selectedUserUploadedImage;
    
                [cell.contentView addSubview:imgv_myImage];
    }
    

    Here define some of the constants:

    #define FONT_SIZE 15.0f
    #define CELL_CONTENT_WIDTH 320.0f /// change this according to your screen size. This is just an example
    #define CELL_CONTENT_MARGIN 10.0f
    

    Now, after creating the labels, you will have to determine the height of the cell in heightForRowAtIndexPath:

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSString *cellText = [arr_text objectAtIndex:indexPath.row];
    
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
        NSString *cellDate = [formatter stringFromDate:[arr_date objectAtIndex:indexPath.row]];
    
       // NSString *text = [items objectAtIndex:[indexPath row]];
    
        CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
    
        //CGSize labelsize = [cellText sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
    
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    
        paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    
        ////for message label
        CGRect textRect = [cellText boundingRectWithSize:constraint
                                             options:NSStringDrawingUsesLineFragmentOrigin
                                          attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:FONT_SIZE], NSParagraphStyleAttributeName: paragraphStyle.copy}
                                             context:nil];
    
        CGSize labelsize = textRect.size;
    
        ////for date label
        CGRect datetextRect = [cellDate boundingRectWithSize:constraint
                                                 options:NSStringDrawingUsesLineFragmentOrigin
                                              attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:FONT_SIZE], NSParagraphStyleAttributeName: paragraphStyle.copy}
                                                 context:nil];
    
        CGSize datelabelsize = datetextRect.size;
    
    
        //CGSize datelabelsize = [cellDate sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
    
        ///combine the height
        CGFloat height = MAX(labelsize.height + datelabelsize.height, 64.0f);
    
        if(height == 64.0f)
        {
           return 74; /// label is of one line, return original/ static height of the cell
        }
    
        else
        {
            return height + 10; /// label is of multi-line, return calculated height of the cell + some buffer height 
        }
    
    }
    

提交回复
热议问题