2016-11-07 3 views
1

以下は、私が構築しようとしているUITableViewCell構造体です。動的コンテンツ用のtableViewCellのAutolayout

enter image description here

上記のタイトルは、私が自動レイアウトせずにUIを生成することができる午前array.Currentlyから来ています。しかし、この場合、私は動的なサイズのセルの高さを得ることができません。

//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; 
} 
} 

は、どのように私はそのようにこれらのラベルをレンダリングする自動レイアウトを使用することができます以下

は、私がこれまでに行うことができるよ何でありますセルの高さは自動的に管理されますか?

他のすべてのセルについては、viewDidLoadにエイメイションされた行の高さを使用しています。ラベルの制約を追加する方法を理解することはできません。

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

私は私が見るにUILabelsの制約を作成する方法を理解することができload.Howeverしなかったことを行っているあなただけのviewDidLoad –

+0

に固定してセルの高さを設定するので逃したと思います。 – icodes

+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 
関連する問題