2016-12-02 4 views
1

Wie UITableViewCell Höhe zur Laufzeit, um die Größe Inhalt nach von Reaktion kommendenObjective-C: Programmgesteuertes UITableViewCell Height-Problem?

ist hier mein Code:

.h

@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>  
@end 

.m

import "ViewController.h" 
import "CustomCellTableViewCell.h" 

static NSString *cellIdentifier = @"cellIdentifier"; 


@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self cofigureTableview]; 
} 

-(void)cofigureTableview 
{ 
    self.table = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; 
    self.table.delegate = self; 
    self.table.dataSource = self; 
    [self.view addSubview:self.table]; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
     return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
     return 2; 
} 

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

    CustomCellTableViewCell *cell = [self.table dequeueReusableCellWithIdentifier:cellIdentifier]; 
    cell = [[CustomCellTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    [self setUpCell:cell atIndexPath:indexPath]; 

    return cell; 
} 

- (void)setUpCell:(CustomCellTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath 
{ 
    if (cell != nil) 
    { 
     UILabel *averageRatingLabel = [[UILabel alloc]init]; 
     averageRatingLabel.text = @"Average Rate"; 
     averageRatingLabel.textAlignment = NSTextAlignmentLeft; 
     averageRatingLabel.numberOfLines = 0; 
     averageRatingLabel.lineBreakMode = NSLineBreakByWordWrapping; 
     averageRatingLabel.textColor = [UIColor blackColor]; 
     averageRatingLabel.translatesAutoresizingMaskIntoConstraints = NO; 
     averageRatingLabel.font = [UIFont systemFontOfSize:12]; 
     [cell.contentView addSubview:averageRatingLabel]; 

     ///// 
     UILabel *sortByLabel = [[UILabel alloc]init]; 
     sortByLabel.text = @"SORT BY Value:"; 
     sortByLabel.textAlignment = NSTextAlignmentLeft; 
     sortByLabel.numberOfLines = 0; 
     sortByLabel.lineBreakMode = NSLineBreakByWordWrapping; 
     sortByLabel.textColor = [UIColor grayColor]; 
     sortByLabel.translatesAutoresizingMaskIntoConstraints = NO; 
     sortByLabel.font = [UIFont systemFontOfSize:10]; 
     [cell.contentView addSubview:sortByLabel]; 


     UILabel *sortByLabel1 = [[UILabel alloc]init]; 
     sortByLabel1.text = @"I want to adjust the row height of a UITableView according to the cell in that row. I want to adjust the row height of a UITableView according to the cell in that row.I want to adjust the row height of a UITableView according to the cell in that row"; 

     sortByLabel1.textAlignment = NSTextAlignmentLeft; 
     sortByLabel1.numberOfLines = 0; 
     sortByLabel1.lineBreakMode = NSLineBreakByWordWrapping; 
     sortByLabel1.textColor = [UIColor grayColor]; 
     sortByLabel1.translatesAutoresizingMaskIntoConstraints = NO; 
     sortByLabel1.font = [UIFont systemFontOfSize:10]; 
     [cell.contentView addSubview:sortByLabel1]; 

     UILabel *sortByLabel2 = [[UILabel alloc]init]; 
     sortByLabel2.text = @"Is there any method to achieve this:"; 
     sortByLabel2.textAlignment = NSTextAlignmentLeft; 
     sortByLabel2.numberOfLines = 0; 
     sortByLabel2.lineBreakMode = NSLineBreakByWordWrapping; 
     sortByLabel2.textColor = [UIColor grayColor]; 
     sortByLabel2.translatesAutoresizingMaskIntoConstraints = NO; 
     sortByLabel2.font = [UIFont systemFontOfSize:10]; 
     [cell.contentView addSubview:sortByLabel2]; 

     UILabel *sortByLabel3 = [[UILabel alloc]init]; 
     sortByLabel3.text = @"This should work because tableView: cellForRowAtIndexPath:"; 
     sortByLabel3.textAlignment = NSTextAlignmentLeft; 
     sortByLabel3.numberOfLines = 0; 
     sortByLabel3.lineBreakMode = NSLineBreakByWordWrapping; 
     sortByLabel3.textColor = [UIColor grayColor]; 
     sortByLabel3.translatesAutoresizingMaskIntoConstraints = NO; 
     sortByLabel3.font = [UIFont systemFontOfSize:10]; 
     [cell.contentView addSubview:sortByLabel3]; 

     UILabel *sortByLabel4 = [[UILabel alloc]init]; 
     sortByLabel4.text = @"then in your tableView: in your viewDidAppearthen in your tableView: cellForRowAtIndexPath: update this variable like your new height and then reload the [self.yourTable reloadData] in your viewDidAppearthen in your tableView: cellForRowAtIndexPath: update this variable like your new height and then reload the [self.yourTable reloadData] in your viewDidAppear:"; 
     sortByLabel4.textAlignment = NSTextAlignmentLeft; 
     sortByLabel4.numberOfLines = 0; 
     sortByLabel4.lineBreakMode = NSLineBreakByWordWrapping; 
     sortByLabel4.textColor = [UIColor grayColor]; 
     sortByLabel4.translatesAutoresizingMaskIntoConstraints = NO; 
     sortByLabel4.font = [UIFont systemFontOfSize:10]; 
     [cell.contentView addSubview:sortByLabel4]; 


     NSDictionary *viewDict1 = NSDictionaryOfVariableBindings(averageRatingLabel); 
     [cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[averageRatingLabel]|" options:0 metrics:0 views:viewDict1]]; 
     [cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-5-[averageRatingLabel]" options:0 metrics:0 views:viewDict1]]; 

     NSDictionary *viewDict2 = NSDictionaryOfVariableBindings(sortByLabel,averageRatingLabel); 
     [cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[sortByLabel]-5-|" options:0 metrics:0 views:viewDict2]]; 
     [cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[averageRatingLabel]-5-[sortByLabel]" options:0 metrics:0 views:viewDict2]]; 

     NSDictionary *viewDict3 = NSDictionaryOfVariableBindings(sortByLabel,sortByLabel1); 
     [cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[sortByLabel1]-5-|" options:0 metrics:0 views:viewDict3]]; 
     [cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[sortByLabel]-5-[sortByLabel1]" options:0 metrics:0 views:viewDict3]]; 

     // 
     NSDictionary *viewDict4 = NSDictionaryOfVariableBindings(sortByLabel1,sortByLabel2); 
     [cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[sortByLabel2]-5-|" options:0 metrics:0 views:viewDict4]]; 
     [cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[sortByLabel1]-5-[sortByLabel2]" options:0 metrics:0 views:viewDict4]]; 

     // 
     NSDictionary *viewDict5 = NSDictionaryOfVariableBindings(sortByLabel2,sortByLabel3); 


     [cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[sortByLabel3]-5-|" options:0 metrics:0 views:viewDict5]]; 
     [cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[sortByLabel2]-5-[sortByLabel3]" options:0 metrics:0 views:viewDict5]]; 

     // 
     NSDictionary *viewDict6 = NSDictionaryOfVariableBindings(sortByLabel3,sortByLabel4); 
     [cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[sortByLabel4]-5-|" options:0 metrics:0 views:viewDict6]]; 
     [cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[sortByLabel3]-5-[sortByLabel4]" options:0 metrics:0 views:viewDict6]]; 

    } 
} 

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

    static CustomCellTableViewCell *cell = nil; 
    static dispatch_once_t onceToken; 

    dispatch_once(&onceToken, ^{ 

     cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    }); 
    [self setUpCell:cell atIndexPath:indexPath]; 

    return [self calculateHeightForConfiguredSizingCell:cell]; 

} 

- (CGFloat)calculateHeightForConfiguredSizingCell:(UITableViewCell *)sizingCell 
{ 

    [sizingCell layoutIfNeeded]; 
    CGSize size = [sizingCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; 
    return size.height; 

} 


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

} 


- (void)didReceiveMemoryWarning 
{ 

    [super didReceiveMemoryWarning]; 

} 

@end 
+0

Welches Problem betrifft die Zellenhöhe? – Poles

+0

Ich möchte die Größe der Zelle zur Laufzeit ändern. Meine Sicht erstellt basierend auf der Serverantwort mit Einschränkungen. –

+0

versuchen Sie eine Think, nachdem Sie Ihre Tabellenansicht in Unteransicht hinzufügen, laden Sie es einfach und überprüfen Sie die Ausgabe. –

Antwort

-1

Verwenden Sie den unten stehenden Code, um ce anzupassen lle Höhe

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
switch(indexPath.row){ 
    case:0 
    return 100; 
    break; 

    case:1 
    return 200; 
    break; 
    } 
} 

Je nach Fall Größe Ihrer Zelle Höhe ändern.

Hoffe das hilft dir.

+0

Ja, aber ich weiß nicht, wie viele Inhalte von der Antwort kommen. Es kann UIButtons oder UILabels usw. sein. Alle diese setzen Constraints beim Überprüfen der letzten Ansicht. Ich möchte die Größe der Zelle zur Laufzeit ändern. –

+0

danke, aber meine Felder können 50 oder mehr sein meine Ansicht ist nicht behoben, es ist veränderbar basierend auf der Antwort. –

+0

Diese Antwort ist korrekt. Es gibt keinen anderen Ort, an dem Sie die Zeilenhöhe für Tabellenansichten außer diesem Delegaten angeben sollten. Wenn Sie möchten, dass es änderbar ist, dann müssen Sie ein Array von Zellenhöhen speichern und sie hier zurückgeben. Wenn Sie Daten empfangen, berechnen Sie die Höhen neu und aktualisieren das heightArray und rufen [tableView reloadData] auf. und deine Zellen werden entsprechend aktualisiert. – GeneCode

0

Verwenden Sie diesen Code, um automatisch Zellenhöhe nach Inhaltsgröße anpassen:

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

- (CGFloat) tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return 300.0f; 
} 

Sie können auch gerne verwenden:

self.tableView.estimatedRowHeight = 300.0; 
self.tableView.rowHeight = UITableViewAutomaticDimension; 
0

Für dynamische Höhe des Inhalts müssen Sie zuerst Inhaltsgröße berechnen Berechnen Sie dann die Höhe entsprechend dieser Höhe und übergeben Sie diese Höhe an Ihre Tabellenansichtszellenhöhe wie folgt:

-(float) getHeightForText:(NSString*) text withFont:(UIFont*) font andWidth:(float) width{ 
    CGSize constraint = CGSizeMake(width , 20000.0f); 
    CGSize title_size; 
    float totalHeight; 
    SEL selector = @selector(boundingRectWithSize:options:attributes:context:); 
    if ([text respondsToSelector:selector]) {     
     title_size = [text boundingRectWithSize:constraint 
options:NSStringDrawingUsesLineFragmentOrigin 
            attributes:@{ NSFontAttributeName : font } 
             context:nil].size; 
     totalHeight = ceil(title_size.height); 
    } else {     
     title_size = [text sizeWithFont:font 
         constrainedToSize:constraint 
          lineBreakMode:NSLineBreakByWordWrapping];     
     totalHeight = title_size.height ;     
    } 
    CGFloat height = MAX(totalHeight, 40.0f); 
    return height;    
} 
Sichtzelle

Pass this Höhe zu Tisch -

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return height; 
} 
+0

Hallo, @neha, deine Logik funktioniert, aber manchmal bekommt mein Label Platz von Oben, wenn ich mein letztes Label schließe, um sonst den Boden zu sehen, wenn ich es freigebe. –

+0

Wie, @ "V: [SortierungByLabel3] -5- [SortierungByLabel4]" ODER @ "V: [SortierungByLabel3] -5- [SortierungByLabel4] -5- |" –

+0

@Sachin, können Sie diese Zeilen in Ihrem cellForRowAtIndex Methode verwenden - für (UIView * subview in [self.contentView Subviews]) { if ([subview isKindOfClass: [UILabel Klasse]]) { [subview removeFromSuperview] ; } } Wenn meine Antwort funktioniert, akzeptieren Sie dies bitte. –

0

enter image description here

Meine Ansicht wie diese, aber ich nicht, dass es Priorität als one-by-one bekannt, ein nach-ein oder Kann UIButton zuerst oder zuletzt sein. Nichts repariert es erstellt basierend auf der Antwort des Servers.

Verwandte Themen