动态告诉 UITableCell UItextview 的高度 [英] Tell UITableCell the height of UItextview Dynamically

查看:32
本文介绍了动态告诉 UITableCell UItextview 的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在制作一个消息应用程序,但最近 tableCells 没有按预期调整大小.我已经使用了新的 iOS 8 相对高度功能,但仍然没有任何变化.到目前为止,它通过 Storyboard 完成的所有设计都按预期工作,但需要告诉 tableCell 根据 textview 高度调整大小.这是它目前的样子.

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

我使用的代码如下.

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

}

为了使 textview 能够展开并看起来像这样,我一直使用的约束如下.

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..?

推荐答案

如果你的 UITableViewCells 由于内容的变化而高度不同,你需要实现

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

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

告诉 UITableView 每行的高度.

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.

这篇关于动态告诉 UITableCell UItextview 的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆