2017-12-22 12 views
0

Ich habe eine benutzerdefinierte Zelle erstellt. Ich habe eine Reihe von Wörterbüchern. Für die Wörterbuchwerte muss ich UILable s erstellen. Jede Zelle kann eine unterschiedliche Anzahl von UILabel s enthalten. Also in meiner Gewohnheit UITableViewCell Klasse habe ich so getan.UITableView Zeilenhöhe nicht ändern

- (void)generateCell { 


BOOL isLandscape = UIInterfaceOrientationIsLandscape(vc.interfaceOrientation); 
for (int i = 0; i < [orderMap count]; i++) { 
    UILabel *lbl = [[UILabel alloc] init]; 
    [lbl setTextColor:[UIColor blueColor]]; 
    [lbl setBackgroundColor:[UIColor yellowColor]]; 
    lbl.tag = [[orderMap objectAtIndex:i] intValue]; 
    [lbl setNumberOfLines:0]; 
    [self.contentView addSubview:lbl]; 
    [lbl setTranslatesAutoresizingMaskIntoConstraints:false]; 

} 

if (isLandscape) { 

} 
else { 
    [self setConstraintsForPortrait]; 
} 
} 

- (void)setConstraintsForPortrait { 


double currentUsedWidth = 0; 
double relativeWidthLimit = 1; 
int reservedPaddingSpace = 20; 
int usableScreenWidthPts = 0; 
int xPositionStartingPoint = 15; 
int xPosition = xPositionStartingPoint; 
int yPosition = INDIVIDUAL_PADDING_PTS; 
int fieldCounter = 0; 
UIView *previousView; 

for (UIView *vw in [self.contentView subviews]) { 
    NSLayoutConstraint *leading = [NSLayoutConstraint constraintWithItem:vw attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:(xPosition + INDIVIDUAL_PADDING_PTS)]; 
    NSLayoutConstraint *top; 

    if (previousView == nil) { 
     top = [NSLayoutConstraint constraintWithItem:vw attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:INDIVIDUAL_PADDING_PTS]; 
    } 
    else { 
     top = [NSLayoutConstraint constraintWithItem:vw attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:previousView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:INDIVIDUAL_PADDING_PTS]; 
    } 

    NSLayoutConstraint *trailing = [NSLayoutConstraint constraintWithItem:vw attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:INDIVIDUAL_PADDING_PTS]; 
    if (fieldCounter >= [orderMap count]) { 
     NSLayoutConstraint *bottom = [NSLayoutConstraint constraintWithItem:vw attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:INDIVIDUAL_PADDING_PTS]; 

     [self.contentView addConstraint:bottom]; 
    } 



    [self.contentView addConstraint:leading]; 
    [self.contentView addConstraint:top]; 
    [self.contentView addConstraint:trailing]; 
    previousView = vw; 
} 

}

Und in meiner UIViewControllerviewDidLoad Methode Ich habe dies getan.

UINib *nib = [UINib nibWithNibName:@"TableViewCell" bundle:[NSBundle mainBundle]]; 
[self.tbl registerNib:nib forCellReuseIdentifier:metadataListTableIdentifier]; 
self.tbl.estimatedRowHeight = 200; 
self.tbl.rowHeight = UITableViewAutomaticDimension; 
[self.tbl reloadData]; 

Warum ändert sich die Zellenhöhe nicht? Bitte hilf mir. Dank

Antwort

0

Nach dem Einstellen Delegierten und Datenquellen von Tableview, Versuchen Sie, diese

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
     return UITableViewAutomaticDimension //return height size whichever you want 
    } 

oder

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { 
    return 200 //return height size whichever you want 
} 
+0

ich es in viewDidLoad gesetzt haben –

+0

noch können Sie versuchen, so zu setzen, weil es nicht in der Art und Weise arbeiten Sie –

+0

gesetzt haben, bemerkte ich diese 2 Zeilen und hinzugefügt, um diese Teilnehmer. Aber keine Wirkung. immer noch gleich :( –

0

Folgen unten Code:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return [self getMessageSize:[[_arrCaseActivityList objectAtIndex:indexPath.row] objectForKey:@"message"] Width:275]; 
} 

-(float)getMessageSize :(NSString *)text Width :(float)textWidth 
{ 
    NSAttributedString *attributedText = 
[[NSAttributedString alloc] 
initWithString:text 
attributes:@ 
{ 
NSFontAttributeName: [UIFont fontWithName:@"Rubik-Regular" size:13.0] 
}]; 

    CGRect rect = [attributedText boundingRectWithSize:(CGSize){textWidth, CGFLOAT_MAX} 
              options:NSStringDrawingUsesLineFragmentOrigin 
              context:nil]; 
    CGSize finalSize = rect.size; 
    return finalSize.height+75; 

} 
+0

gesetzt Aber wenn ich die Einschränkungen die Zelle Größe sollte sich entsprechend ändern, nicht wahr? –

+0

Entfernen Sie benutzerdefinierte Zellen Einschränkungen und versuchen Sie es. Es funktioniert. –

0

Klick + Ziehen Sie den Tableview und legen Viewcontroller Ansicht gleiche Breite und gleiche Höhe

#pragma mark tableview delegate 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return [DATA count]; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    PartyListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 
    if (cell == nil) { 
     // Load the top-level objects from the custom cell XIB. 
     NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"PartyListCell" owner:self options:nil]; 
     cell = [topLevelObjects objectAtIndex:0]; 
    } 

    cell.textLabel.text = [DATA objectAtIndex:indexPath.row]; 

    return cell; 
} 

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    return 38.0; 
} 

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    } 

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ 
    UIView *tableviewHeader = [[[NSBundle mainBundle] loadNibNamed:@"PartyListHeaderView" owner:self options:nil] objectAtIndex:0]; 
    return tableviewHeader; 
} 

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ 
    return 38.0; 
} 

Klick auf den Button laden Sie nib Datei

if (popUpView== nil) { 
     popUpView = [[[NSBundle mainBundle] loadNibNamed:@"PopupView" owner:self options:nil] objectAtIndex:0]; 
     [self.view addSubview:popUpView]; 
     [popUpView setDelegate:self]; 
     [popUpView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
    } 
+0

Mystack ich denke, Sie wollen um das Gerät zu drehen, versuchen Sie es. –

+0

fertig. Aber meine Zelle Höhe ändert sich nicht –

+0

hier PartyListCell ist meine benutzerdefinierte Zelle jetzt versuchen Sie diesen Code –

0

richtigen Einschränkungen im Interface Builder zB sorgen. leading, trailing usw. und setzen Sie no of lines für UILabel als . Fügen Sie den folgenden Code in viewDidLoad Methode hinzu.

self.tableView.rowHeight = UITableViewAutomaticDimension 
self.tableView.estimatedRowHeight = 300 // estimated row height for performance improvement 
+0

haben schon diese aber keine Wirkung gesetzt –

Verwandte Themen