Increase the row height of the main table according to the custom cell

I have an application in which I have a Tableview , and each row is scanned in this table. I am dynamically creating my own tableview cell.

Below is the code for this.

NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"flowviewTableViewCell" owner:self options:nil];

cell2 = [nib objectAtIndex:0];

return cell2;

"FlowTableViewCell" is a UITableViewCell. In this user cell, I have one table view.

I show some data in my own tableview cell from an array, and this data varies in length. It is not fixed.

I can increase the size of the user cell, but not the main row height of the table, depending on the size of the user cell in the table.

I want to increase the height of the main table cell size dynamically depending on the size of the user table cell.

In the following code, the height of the custom tableView cell is increased.

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

    NSString *str = [arrComments objectAtIndex:indexPath.row];
    CGSize size = [str sizeWithFont:[UIFont fontWithName:@"Helvetica" size:14] constrainedToSize:CGSizeMake(280, 999) lineBreakMode:NSLineBreakByWordWrapping];
    NSLog(@"%f",size.height);
    if (size.height<20)

    {
        size.height=20;
        //m= size.height;

    }

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

    return size.height +  30;


}

How to adjust the height of the main height of the table table depending on the size of the user table tableviewcell?

Here I am attaching some screenshots for a clear understanding.

Below is my custom TableViewCell:

Custom TableViewCell

Below is my main TableView:

Main tableview

The following is the output that I am getting right now:

output getting right now

In the above image, you can see that comment2 gets cut, and comment3 of the same message is displayed in the next message.

I need the output as shown below.

enter image description here

So my question is how to dynamically increase the height of the main table cell size depending on the size of the user table cell?

, .

+4
7

. . .

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];
}

:

#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

, , 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 
    }

}
+3

, :

- (CGRect)heightOfLabel:(UILabel*)resizableLable
 {
    CGSize constrainedSize = CGSizeMake(resizableLable.frame.size.width  , 9999);

        NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                              [UIFont fontWithName:@"HelveticaNeue" size:11.0], NSFontAttributeName,
                                              nil];

        NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"textToShow" attributes:attributesDictionary];

        CGRect requiredHeight = [string boundingRectWithSize:constrainedSize options:NSStringDrawingUsesLineFragmentOrigin context:nil

        ];


        if (requiredHeight.size.width > self.resizableLable.frame.size.width) {
            requiredHeight = CGRectMake(0,0, self.resizableLable.frame.size.width, requiredHeight.size.height);
        }

      return requiredHeight;
    }

TableView:

- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {
   UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

   return [self heightOfLabel:cell.textLabel];

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

NSString *str = [arrComments objectAtIndex:indexPath.row];
UIFont *font = [UIFont fontWithName:@"Helvetica" size:14];
CGRect new = [str boundingRectWithSize:CGSizeMake(280, 999) options:NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: font} context:nil];
CGSize size= new.size;
NSLog(@"%f",size.height);
if (size.height<20)

{
    size.height=20;
    //m= size.height;

}

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

return size.height +  30;


}
0

:

  • UITableViewCell (, FlowViewAbstractTableViewCell)
  • , + (CGFloat)sizeForCellWithComment:(Comment *)comment. , , - , .
  • , , , , FlowViewAbstractTableViewCell. , FlowViewTextCommentTableViewCell FlowViewPictureCommentTableViewCell.
  • +sizeForCellWithComment:. , .
  • - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath UITableViewDelegate. , . :

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { Comment *commentForCell = [self.comments objectAtIndex:indexPath.row]; return [CommentTableViewCell sizeForComment:commentForCell].height; }

. , , MVC.

0

UITableViewCells. , UITableViewCell , .

0

Very simple solution than above

0
source

try it

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
 return [self.tableName.dataSource tableView:self.tableName cellForRowAtIndexPath:indexPath].contentView.frame.size.height;
}

And in the cellForRowAtIndexPath delegate, if you use a shortcut then first set the number of lines to be marked as 0, then set the label text, and then add its sizeTofit property. Like this

 [cell.yourLabelName setNumberOfLines:0];
 cell.yourLabelName.text = @"Your long or short text";
 [cell.yourLabelName sizeToFit];

This will expand the height of your label to match the text in it.

After that, you can set the size of your cellView content this way.

 cell.contentView.frame = CGRectMake(cell.contentView.frame.origin.x, cell.contentView.frame.origin.y, cell.contentView.frame.size.width,"Height of label" + cell.contentView.frame.size.height);
0
source

All Articles