如何使用自动布局基于数据大小在tableview单元格上增加UItextview

时间:2015-08-19 03:46:30

标签: ios swift

您好我是ios的初学者,在我的项目中,我在tableview单元格中添加了两个textview,他们需要根据数据大小进行增长

根据我的代码,第一个textview没有增长,第二个textview正在根据数据大小增长,我在这做错了什么,请帮我一个

以下是代码:

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

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

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

        NSDictionary * views = NSDictionaryOfVariableBindings(formulaText,formulaText1);

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

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

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

        [cell.contentView addConstraints:formulaH];
        [cell.contentView addConstraints:formulaH1];
        [cell.contentView addConstraints:textFieldConstraintV];
}

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

    return UITableViewAutomaticDimension;
}

1 个答案:

答案 0 :(得分:0)

你必须自己快速转换我的代码。

我的代码不是基于约束的B'coz我没有得到约束,但逻辑对我来说是完美的,没有约束。

在下面的代码中,我在单元格中有一个标签,它会变长,它的位置是x = 20,y = 5,width = 255,height = growable。

你可以使用heightForRowAtIndexpath方法

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //get string from array using indexpath 
    CGSize newLabelsize = [strComment sizeWithFont:[UIFont fontWithName:@"CenturyGothic" size:10] constrainedToSize:CGSizeMake(width, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];

    //Give your font and it's size for calculating data size.
   //in the above strComment is a string put in textview and 
  //width of your textview = width and
  //MAXFLOAT = height for grow and it is a macro so you can put it directly.
  //now you have new size of the textview so calculate with other outlet and return new height of the cell.

    return 5+newLabelsize.height+5; 
    // last +5 is for bottom space of label.
    // first 5+ is y pos of you of label.

}

上面的代码只是修复你的单元格大小

现在,在用于设置textview大小的cellforrow方法中进行相同的计算

CGSize newLabelsize = [strComment sizeWithFont:[UIFont fontWithName:@"CenturyGothic" size:10] constrainedToSize:CGSizeMake(width, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];

cell.textview.frame = CGRectMake(cell.textview.frame.origin.x,cell.textview.frame.origin.y,cell.textview.frame.size.width,newLabelsize.height);

现在你终于得到了你想要的。 希望这对你有所帮助。

谢谢你!