Tell UITableCell the height of UItextview Dynamica

2019-08-30 09:39发布

Currently I'm making a Messaging App but recently the tableCells are not resizing as expected. I've used the new iOS 8 relative height functionality but still nothing changes. All the design it's being done via Storyboard so far everything works as expected but need to tell the tableCell to resize based on the textview height. here how it's currently looking.

http://i.imgur.com/lqoxxJV.png

The Code I'm using is the following.

    - (UITableViewCell*)tableView:(UITableView*)table cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSDictionary* chatMessage = [_conversation objectAtIndex:indexPath.row];

    // Show If type is String
    if ([chatMessage[@"type"] isEqualToString:@"string"]) {

        // if its me
        if([chatMessage[@"user_sender"] isEqualToString:_user_sender]){



            NSString *CellIdentifier = @"fromMe";
            FromMeTableViewCell *cell = [table dequeueReusableCellWithIdentifier:CellIdentifier];
            [cell.message_me setNeedsLayout];
            [cell.message_me layoutIfNeeded];
            cell.message_me.clipsToBounds = YES;
            cell.message_me.textContainerInset = UIEdgeInsetsMake(8.0, 8.0, 7.0, 8.0);
            cell.message_me.text = chatMessage[@"msg"];
            cell.message_date_me.text = [NSString stringWithFormat:@"%@", chatMessage[@"message_date"]];
            cell.avatar_message_me.clipsToBounds = YES;
            cell.avatar_message_me.layer.cornerRadius = cell.avatar_message_me.frame.size.width /2;
            cell.avatar_message_me.image = [UIImage imageNamed:@"amber.png"];

            return cell;

        }
        // it its other user
        else {

            NSString *CellIdentifier = @"fromThem";
            FromThemTableViewCell *cell = [table dequeueReusableCellWithIdentifier:CellIdentifier];
            [cell.message_them setNeedsLayout];
            [cell.message_them layoutIfNeeded];
            cell.message_them.clipsToBounds = YES;
            cell.message_them.textContainerInset = UIEdgeInsetsMake(8.0, 8.0, 7.0, 8.0);
            cell.message_them.text = chatMessage[@"msg"];
            cell.message_date_them.text = [NSString stringWithFormat:@"%@", chatMessage[@"message_date"]];
            cell.avatar_message_them.clipsToBounds = YES;
            cell.avatar_message_them.layer.cornerRadius = cell.avatar_message_them.frame.size.width /2;
            cell.avatar_message_them.image = [UIImage imageNamed:@"jenny.png"];

            return cell;

        }
    }

    // Show if type is Image File

        else if ([chatMessage[@"type"] isEqualToString:@"img"]){

            // if its me
            if(![chatMessage[@"user_sender"] isEqualToString:_user_sender]){

                NSString *CellIdentifier = @"fromThemImage";
                FromThemImageTableCell *cell = [table dequeueReusableCellWithIdentifier:CellIdentifier];
                cell.image_type_them.image = [UIImage imageNamed:@"foto.jpeg"];
                cell.message_date_them.text = [NSString stringWithFormat:@"%@", chatMessage[@"message_date"]];
                return cell;

            }
            // if its other user
            else {

                NSString *CellIdentifier = @"fromMeImage";
                FromMeImageTableCell *cell = [table dequeueReusableCellWithIdentifier:CellIdentifier];
                cell.image_type_me.image = [UIImage imageNamed:@"foto.jpeg"];
                cell.message_date_me.text = [NSString stringWithFormat:@"%@", chatMessage[@"message_date"]];
                return cell;
            }


        }

    else if ([chatMessage[@"type"] isEqualToString:@"file"]){

        NSLog(@"Type: %@", chatMessage[@"type"]);

        NSString *CellIdentifier = @"fromThemFile";
        FromThemFileTableCell *cell = [table dequeueReusableCellWithIdentifier:CellIdentifier];
        cell.message_date_them.text = [NSString stringWithFormat:@"%@", chatMessage[@"message_date"]];
        cell.file_name_them.text = chatMessage[@"msg"];

        cell.file_type_them.contentMode = UIViewContentModeScaleAspectFill;

        NSArray *formats = [chatMessage[@"msg"] componentsSeparatedByString:@"."];

        // PDF Format

        if ([formats[1] isEqualToString:@"pdf"]) {
            cell.file_type_them.image = [UIImage imageNamed:@"pdf.png"];
        }

        // Word Format

        if ([formats[1] isEqualToString:@"doc"]) {
            cell.file_type_them.image = [UIImage imageNamed:@"doc.png"];
        }

        if ([formats[1] isEqualToString:@"docx"]) {
            cell.file_type_them.image = [UIImage imageNamed:@"doc.png"];
        }

        // Power Point Format

        if ([formats[1] isEqualToString:@"pptx"]) {
            cell.file_type_them.image = [UIImage imageNamed:@"ppt.png"];
        }

        if ([formats[1] isEqualToString:@"ppt"]) {
            cell.file_type_them.image = [UIImage imageNamed:@"ppt.png"];
        }

        // Excel Format

        if ([formats[1] isEqualToString:@"xls"]) {
            cell.file_type_them.image = [UIImage imageNamed:@"xls.png"];
        }

        if ([formats[1] isEqualToString:@"xlsx"]) {
            cell.file_type_them.image = [UIImage imageNamed:@"xls.png"];
        }


        return cell;


    }

    else {

        // Remember to set this as a default value
        NSString *CellIdentifier = @"fromThem";
        FromThemTableViewCell *cell = [table dequeueReusableCellWithIdentifier:CellIdentifier];
        cell.message_them.text = chatMessage[@"msg"];
        cell.message_date_them.text = [NSString stringWithFormat:@"%@", chatMessage[@"message_date"]];
        return cell;
    }

}

The constraints I've been using for the textview to be able to expand and look like this is the following.

http://i.imgur.com/Kjva3ES.png

Any Idea how to fix this on a decent way..?

1条回答
smile是对你的礼貌
2楼-- · 2019-08-30 10:06

If your UITableViewCells vary in height due to variations in content you need to implement

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

to tell UITableView the height for each row.

In one of my apps this mainly involved calling

- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context;

to predict the area needed by my expanding text field, and then adding in some margin.

查看更多
登录 后发表回答