2016-11-07 2 views
1

Unten ist die UITableViewCell Struktur, die ich bauen möchte.Autolayout in tableViewCell für dynamischen Inhalt

enter image description here

Die Titel oben kommen aus einem array.Currently Ich kann den UI ohne automatisches Layout erzeugen. In diesem Fall bin ich jedoch nicht in der Lage, eine dynamisch große Zellenhöhe zu erhalten.

Unten ist das, was ich in der Lage bin, so weit zu tun -

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

und Tabellenansicht Zellenhöhe wie unten geführt wird -

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

Wie kann ich automatisches Layout verwenden, um diese Etiketten zu machen, so dass Die Höhe der Zelle wird automatisch verwaltet?

Für alle anderen Zellen - ich verwende esimated Zeilenhöhe in viewDidLoad .Weitere nicht in der Lage zu verstehen, wie Sie Einschränkungen für Etiketten hinzufügen.

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

Ich glaube, Sie nur die Einstellung Zellenhöhe verpasste wie in viewDidLoad feste –

+0

Ich habe das nicht in der Lage load.However zu verstehen, wie man schreibt Einschränkungen für UILabels in View getan habe . – icodes

+0

Tut mir leid, aber ich bin nicht mit dynamischen Einschränkungen vertraut –

Antwort

0

Hope this Hilfe

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 
Verwandte Themen