2016-09-02 44 views
0

我正在使用一個沒有任何故事板的舊Objective-C項目。我需要一個UITableView來顯示含有3個標籤的UITableViewCell的結果。任務是在UITableviewCellObjective C在代碼中設置自動行高度

這裏垂直對齊3個標籤是我萌生標籤:

serviceTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 200, LONG_MAX)]; 
[serviceTitleLabel setFont:[UIFont SingPostRegularFontOfSize:16.0f fontKey:kSingPostFontOpenSans]]; 
[serviceTitleLabel setNumberOfLines:0]; 
[serviceTitleLabel setTextColor:RGB(51, 51, 51)]; 
[serviceTitleLabel setBackgroundColor:[UIColor clearColor]]; 
[serviceTitleLabel setTextAlignment:NSTextAlignmentLeft]; 
[contentView addSubview:serviceTitleLabel]; 

statusLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 30, 200, 30)]; 
[statusLabel setFont:[UIFont SingPostBoldFontOfSize:12.0f fontKey:kSingPostFontOpenSans]]; 
[statusLabel setTextColor:RGB(125, 136, 149)]; 
[statusLabel setBackgroundColor:[UIColor clearColor]]; 
[contentView addSubview:statusLabel]; 

costLabel = [[UILabel alloc] initWithFrame:CGRectMake(216, 20, 85, 30)]; 
costLabel.right = contentView.right - 15; 
[costLabel setFont:[UIFont SingPostBoldFontOfSize:16.0f fontKey:kSingPostFontOpenSans]]; 
[costLabel setTextColor:RGB(51, 51, 51)]; 
[costLabel setTextAlignment:NSTextAlignmentRight]; 
[costLabel setBackgroundColor:[UIColor clearColor]]; 
[contentView addSubview:costLabel]; 

我們怎樣才能在UITableViewController調整使得其尺寸將根據標籤尺寸大小?需要在cell和tableview中設置?任何幫助深表感謝。謝謝。

這是現在的屏幕。看不對

enter image description here

+0

它不是我清楚,如果你只有一個的tableView有一個單元或多個單元格 – ddb

+0

你好這是一個動態的tableview與數據從一系列的項目(3標籤是項目屬性) –

+0

我需要了解的是,如果每個單元格應該有其自定義高度或計算出的高度是有效的每個細胞 – ddb

回答

2

你必須使用UITableViewAutomaticDimension管理單元格高度按內容。爲此,您必須從各個方面設置標籤的約束條件,即單擊PIN碼,然後取消選中Constraint from margin複選框,併爲兩個標籤添加從頂部,底部,左側和右側的約束。如果你想使用UITableViewAutomaticDimension

現在在viewDidLoad方法ViewController添加這兩個線路

self.tableView.estimatedRowHeight = 44.0; 
self.tableView.rowHeight = UITableViewAutomaticDimension; 
+0

謝謝我的概率是我沒有故事板。都需要在代碼中完成 –

1

的解決方案取決於您的iOS部署目標。如果它比iOS 8高於或等於:

self.tableView.rowHeight = UITableViewAutomaticDimension; 

此解決方案適用於單元格設置正確的情況。

如果它比你需要自己來計算的話下,實施的UITableViewDelegate

- tableView:heightForRowAtIndexPath: 
+0

嗨,感謝您的幫助,我在哪裏放置該行self.tableView.rowHeight = UITableViewAutomaticDimension ;?無論如何設置細胞的約束。在單元格中只有3個標籤 –

+0

在你的tableview控制器中設置self.tableView.rowHeight(例如viewDidLoad)。關於約束:你應該爲所有標籤設置約束,每個表都應該對所有邊都有約束(頂部,右邊,底部,左邊) – Konstantin

+0

因此需要爲代碼中的所有3標籤設置包含? 4爲1個標籤。 12包含3個標籤?我正在嘗試堆棧視圖方法。希望可以簡單 –