2016-08-01 59 views
0

Ich habe eine benutzerdefinierte UITableViewCell mit einer Textansicht.UITableViewCell Höhe kann nach textView dynamisch nicht erhöht werden

Die textView-Höhe erhöht sich entsprechend ihrem Text.

Ich kann nicht herausfinden, wie man die Zellenhöhe erhöht, um der Textansicht zu entsprechen.

Hier ist mein Code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NotificationCell *cell = (NotificationCell *)[tableView dequeueReusableCellWithIdentifier:@"cell"]; 
    if (cell == nil) { 
     NSArray *ary = [[NSBundle mainBundle]loadNibNamed:@"NotificationCell" owner:self options:nil]; 
     cell = [ary objectAtIndex:0]; 

    } 
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:[NotificationArray objectAtIndex:indexPath.section]]; 
    cell.title_lbl.text=[dict objectForKey:@"event_title"]; 
    cell.description_lbl.text=[dict objectForKey:@"description"]; 
    [cell.description_lbl sizeToFit]; 
    [cell sizeToFit]; 

    return cell; 
} 
+0

Possible Duplikat [Höhe tableview Zelle erhöhen nach Menge des UILabel Text] (http://stackoverflow.com/questions/36544115/increase-height-of-tableview-cell-according-to-amount-of- Uilabel-Text) –

Antwort

1

Für, dass Sie diese Schritte folgen müssen.

Schritt 1

Verwenden Autolayout in Ihrem storyboard oder .xib und Ihre Constraints für das Label stellen Sie sicher, die so sein dynamisch sollte.

enter image description here

Schritt 2

Auf viewDidLoad Ihrer Viewcontroller Sie estimatedRowHeight so zu bieten haben.

self.tblViewOutlet.rowHeight = UITableViewAutomaticDimension; 
self.tblViewOutlet.estimatedRowHeight = 50.0; //any height which is like minimum 

Schritt 3

Und in Tableview Delegierten heightForRowAtIndexPath,

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

Stellen Sie sicher, Ihre UILabel (hier Ich denke, description_lbl) von Prototyp-Zelleneigenschaft numberOfLines sollte 0 sein.

Ich hoffe, es funktioniert für Sie. :)

Glückliche Kodierung !!