Ios 在tableViewCell中自动布局动态内容

Ios 在tableViewCell中自动布局动态内容,ios,uitableview,ios-autolayout,Ios,Uitableview,Ios Autolayout,下面是我希望构建的UITableViewCell结构 上面的标题来自一个数组。目前我能够生成没有自动布局的UI。但是在这种情况下,我无法获得动态大小的单元格高度 以下是我到目前为止能做的事情- //Second cell case 2:{ UIView *thirdContainer = [[UIView alloc]initWithFrame:CGRectZero]; [cell.contentView addSubview:thirdContainer]; UIL

下面是我希望构建的
UITableViewCell
结构

上面的标题来自一个数组。目前我能够生成没有自动布局的UI。但是在这种情况下,我无法获得动态大小的单元格高度

以下是我到目前为止能做的事情-

  //Second cell
 case 2:{
   UIView *thirdContainer = [[UIView alloc]initWithFrame:CGRectZero];
   [cell.contentView addSubview:thirdContainer];

   UILabel *titleLabel2 = [[UILabel alloc]initWithFrame:CGRectMake(8, 8, 220, 20)];
   titleLabel2.text = @"Features:";
   [thirdContainer addSubview:titleLabel2];

   NSDictionary *thirdDict = @{@"thirdContainer":thirdContainer,@"titleLabel2":titleLabel2};

  [thirdContainer setTranslatesAutoresizingMaskIntoConstraints:NO];
  NSArray *thirdContainerWdt = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[thirdContainer]|" options:0 metrics:nil views:thirdDict];
  NSArray *thirdContainerHT = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[thirdContainer]|" options:0 metrics:nil views:thirdDict];
  [cell.contentView addConstraints:thirdContainerWdt];
  [cell.contentView addConstraints:thirdContainerHT];

  // Without Autolayout
        int x;
        int y = 10;
        for (int i=0; i<_featuresArray.count; i++) {

            if (i%2==0) {
                x = 8;
                y = y + 18;
            }else{
                x = 200;

            }
            UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(x, y,160, 18)];

            lbl.text = _featuresArray[i];
            [thirdContainer addSubview:lbl];
        }
 }
如何使用autolayout渲染这些标签,以便自动管理单元格的高度

对于所有其他单元格-我在
viewDidLoad
中使用了esimated行高。但是,我无法理解如何为标签添加约束

_myTableView.estimatedRowHeight = 168.0;
_myTableView.rowHeight = UITableViewAutomaticDimension;
希望这有帮助

NSInteger numberOfLines=features.count >> 1;
UILabel * prevLeftLabel=nil;
UILabel * prevRightLabel=nil;

NSInteger verticalHuggingPriority=UILayoutPriorityDefaultHigh;

for(NSInteger line=0;line<numberOfLines;line++){

    UILabel * leftLabel=[UILabel new];
    UILabel * rightLabel=(line << 1 + 1<features.count ? [UILabel new] : nil);

    [thirdView addSubview:leftLabel];
    if(rightLabel)
        [thirdView addSubview:rightLabel]; 

    if(prevLeftLabel){
        //add top constraint to previous left label
    }else{
        //add top constraint to container view
    }

    if(prevRightLabel){
        //add top constraint to previous right label
    }else{
        //add top constraint to container view
    }

    //add leading constraint from container to the left label

    if(rightLabel){
        //add constraint between left and right labels
        //add trailing constraint from the right label to container
    }else{
       //add trailing constraint from left label to container
    }

    [leftLabel setContentHuggingPriority:verticalPriority
                                 forAxis: UILayoutConstraintAxisVertical];

    if(rightLabel)
        [rightLabel setContentHuggingPriority:verticalPriority
                                     forAxis: UILayoutConstraintAxisVertical];

    verticalPriority++;

    prevLeftLabel=leftLabel;
    prevRightLabel=rightLabel;
}

//After loop finished set bottom constraints from last label to container
//The labels are stored in prevLeftLabel and prevRightLabel at this point
NSInteger numberOfLines=features.count>>1;
UILabel*prevLeftLabel=nil;
UILabel*prevRightLabel=nil;
NSInteger垂直拥抱优先级=UILayoutPriorityDefaultHigh;
对于(NSInteger line=0;line希望这有帮助

NSInteger numberOfLines=features.count >> 1;
UILabel * prevLeftLabel=nil;
UILabel * prevRightLabel=nil;

NSInteger verticalHuggingPriority=UILayoutPriorityDefaultHigh;

for(NSInteger line=0;line<numberOfLines;line++){

    UILabel * leftLabel=[UILabel new];
    UILabel * rightLabel=(line << 1 + 1<features.count ? [UILabel new] : nil);

    [thirdView addSubview:leftLabel];
    if(rightLabel)
        [thirdView addSubview:rightLabel]; 

    if(prevLeftLabel){
        //add top constraint to previous left label
    }else{
        //add top constraint to container view
    }

    if(prevRightLabel){
        //add top constraint to previous right label
    }else{
        //add top constraint to container view
    }

    //add leading constraint from container to the left label

    if(rightLabel){
        //add constraint between left and right labels
        //add trailing constraint from the right label to container
    }else{
       //add trailing constraint from left label to container
    }

    [leftLabel setContentHuggingPriority:verticalPriority
                                 forAxis: UILayoutConstraintAxisVertical];

    if(rightLabel)
        [rightLabel setContentHuggingPriority:verticalPriority
                                     forAxis: UILayoutConstraintAxisVertical];

    verticalPriority++;

    prevLeftLabel=leftLabel;
    prevRightLabel=rightLabel;
}

//After loop finished set bottom constraints from last label to container
//The labels are stored in prevLeftLabel and prevRightLabel at this point
NSInteger numberOfLines=features.count>>1;
UILabel*prevLeftLabel=nil;
UILabel*prevRightLabel=nil;
NSInteger垂直拥抱优先级=UILayoutPriorityDefaultHigh;

对于(NSInteger line=0;line我想您刚刚错过了将单元格高度设置为ViewDidLoad中的固定值我已经在View中完成了该设置。但是我无法理解如何为UILabels编写约束。抱歉,但我无法接受动态约束我想您刚刚错过了将单元格高度设置为ViewDidLoad中的固定值我已经在View中完成了该设置加载。但是无法理解如何为UILabels编写约束。抱歉,我无法接受动态约束