如何使用自动布局动态修复tableview单元格和textview高度

时间:2015-07-22 10:18:44

标签: ios objective-c

嗨我是Ios的初学者在我的项目中我使用自动布局来创建textview和tableview并根据我的要求tableview单元格和textview高度正在修复基于文本数据和在我的phone4 @ 5它没关系但是来了下来到iphone 6英寸一些空的文本空间即将到来,但我想删除textview空白空间,并且还必须根据文本数据减少tableview单元格高度我尝试了我的水平最好,但它不会来请帮帮我

根据我的代码我手机6屏幕就像第二屏幕,但我想修复tableview单元格和textview高度像所有我手机英寸的第一屏幕请帮助我我试图解决这个因为很多天,但确切的结果是不来了

enter image description here

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cellIdentifier";

    UITableViewCell *cell =
    [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];



    if(indexPath.section == 1)
    {

        formulaText = [[UITextView alloc]init];
        formulaText.font = [UIFont fontWithName:@"Bitter-Regular" size:15.0f];
        formulaText.translatesAutoresizingMaskIntoConstraints = NO;
        formulaText.backgroundColor = [UIColor lightGrayColor];
        formulaText.scrollEnabled = NO;
        [cell.contentView addSubview:formulaText];

        NSDictionary * views = NSDictionaryOfVariableBindings(formulaText);

        NSArray * formulaH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[formulaText]-10-|"
                                                                        options:0
                                                                        metrics:nil
                                                                          views:views];

        NSArray * formulaV = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-5-[formulaText]-|"
                                                                     options:0
                                                                     metrics:nil
                                                                       views:views];

        [cell.contentView addConstraints:formulaH];
        [cell.contentView addConstraints:formulaV];

        formulaText.text = [formulaArray objectAtIndex:0];
        formulaText.font = [UIFont fontWithName:@"Bitter-Regular" size:15.0];

    }

    if (indexPath.section == 2)
    {
        aboutText = [[UITextView alloc]init];
        aboutText.font = [UIFont fontWithName:@"Bitter-Regular" size:15.0f];
        aboutText.translatesAutoresizingMaskIntoConstraints = NO;
        aboutText.backgroundColor = [UIColor darkGrayColor];
        aboutText.scrollEnabled = NO;
        [cell.contentView addSubview:aboutText];

        NSDictionary * views = NSDictionaryOfVariableBindings(aboutText);

        NSArray * formulaH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[aboutText]-10-|"
                                                                     options:0
                                                                     metrics:nil
                                                                       views:views];

        NSArray * formulaV = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-5-[aboutText]-|"
                                                                     options:0
                                                                     metrics:nil
                                                                       views:views];

        [cell.contentView addConstraints:formulaH];
        [cell.contentView addConstraints:formulaV];

        aboutText.text = [aboutArray objectAtIndex:0];
        aboutText.font = [UIFont fontWithName:@"Bitter-Regular" size:15.0];

    }

    return cell;
}


- (CGSize)calculateHeightForString:(NSString *)str
{
    CGSize size = CGSizeZero;

    UIFont *labelFont = [UIFont systemFontOfSize:17.0f];
    NSDictionary *systemFontAttrDict = [NSDictionary dictionaryWithObject:labelFont forKey:NSFontAttributeName];

    NSMutableAttributedString *message = [[NSMutableAttributedString alloc] initWithString:str attributes:systemFontAttrDict];
    CGRect rect = [message boundingRectWithSize:(CGSize){320, MAXFLOAT}
                                        options:NSStringDrawingUsesLineFragmentOrigin
                                        context:nil];//you need to specify the some width, height will be calculated
    size = CGSizeMake(rect.size.width, rect.size.height + 5); //padding

    return size;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //return 200;

    if([indexPath section] == 0)
    {
        return  200;
    }

    else if([indexPath section] == 1)
    {
        NSString *cellText = [formulaArray objectAtIndex:0];
        UIFont *cellFont = [self fontForCell];
        CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
        CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
        return labelSize.height + 20;
    }

    else
    {
        NSString *cellText = [aboutArray objectAtIndex:0];
        UIFont *cellFont = [self fontForCell];
        CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
        CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
        return labelSize.height + 20;
    }
}

enter image description here

1 个答案:

答案 0 :(得分:0)

创建您的UITableViewCell - 例如使用xib的ContinentUITableViewCell。在那里设置你的元素并根据需要设置autolayout。在你的UITableView中注册它,并设置重用标识符。

接下来要用:

替换你的heightForRowAtIndexPath方法
Embed App Extensions

另外,在你的- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewAutomaticDimension } 中设置self.tableView.estimatedHeight到平均高度(例如200)。