2016-08-01 61 views
0

我有一個帶文本視圖的自定義UITableViewCell無法根據textView動態增加UITableViewCell高度

textView的高度根據其文字增加。

我不知道如何增加單元格高度以適應文本視圖。

這裏是我的代碼:

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

[根據UILabel文本數量增加tableview單元格的高度]可能的副本(http://stackoverflow.com/questions/36544115/increase-height-of-tableview-cell-according-to-amount-of-的UILabel文本) –

回答

1

對於您需要按照這個步驟。

步驟1

使用Autolayoutstoryboard.xib,並確保您的約束是動態的標籤應該是這樣的。

enter image description here

步驟2

在您的視圖 - 控制的viewDidLoad你必須提供estimatedRowHeight這樣。

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

步驟3

而且在委託的tableView heightForRowAtIndexPath,

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

確保您UILabel(在這裏我想description_lbl)原型單元屬性numberOfLines應該是0

希望它適合你。 :)

快樂編碼!