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

내가있는 viewDidLoad –

+0

에 고정으로 그냥 내가보기에 UILabels에 대한 제약을 작성하는 방법을 이해하기 load.However이 수하지 않았다 짓을 설정 셀 높이의 놓친 생각 . – icodes

+0

죄송합니다. 그러나 나는 동적 인 제약 조건과 함께 comfertable이 아닙니다. –

답변

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