tableViewCell中的Autolayout用于动态内容

时间:2016-11-07 10:48:04

标签: ios uitableview ios-autolayout

以下是我要构建的UITableViewCell结构。

enter image description here

上面的标题来自一个数组。目前我能够生成没有自动布局的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];
        }
 }

表视图单元格高度的管理方式如下 -

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

if (indexPath.row==2) {
    if (_featuresArray.count==1||_featuresArray.count==2) {
        return 50;
    }else{
        float ht = _featuresArray.count;
        ht = ht*25;  
        return ht;
    }

}else{
    return UITableViewAutomaticDimension;
 }
}

如何使用autolayout渲染这些标签,以便自动管理单元格的高度?

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

_myTableView.estimatedRowHeight = 168.0;
_myTableView.rowHeight = UITableViewAutomaticDimension;

1 个答案:

答案 0 :(得分:0)

希望这个帮助

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