动态地告诉UITableCell UItextview的高度

时间:2014-12-10 21:26:02

标签: ios uitextview tablecell

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

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能够扩展的约束如下所示。

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

任何想法如何以体面的方式解决这个问题..?

1 个答案:

答案 0 :(得分:0)

如果您的UITableViewCells由于您需要实施的内容变化而导致身高变化

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

告诉UITableView每行的高度。

在我的一个应用程序中,这主要涉及调用

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

预测扩展文本字段所需的区域,然后添加一些边距。