試圖瞭解何時設置單元高度。 我有一個在佈局中創建的UITableView,而不是代碼。UITableView單元高度設置生命週期
然後:
#define TABLE_VIEW_ROW_HEIGHT 100.0
跟:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"Setting row height");
return TABLE_VIEW_ROW_HEIGHT;
}
和
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"Row height: %f", tableView.rowHeight);
// ... other happy stuff that does not change row height but could sure use the real row height
最後的NSLog報告44,而不是100。這意味着,該方法中的代碼,需要獲得行高不是使用真實的高度。
我已驗證之前
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
運行和,因此,行是所需大小的,而不是44
默認大小我是否需要在其他地方明確設定高度?
編輯時,做一些研究:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
...更適當地用於設置行的高度時,不是所有的行都是相同的高度。我錯誤地使用它來將所有行設置爲相同的高度。
做正確的方法是調用
the_tableview.rowHeight = TABLE_VIEW_ROW_HEIGHT;
...從什麼地方像viewDidLoad中。我其實(代碼省略),但沒有奏效。今天晚上我意識到這是圖形佈局工具的另一個實例......我忘了將IBOutlet連接到UITableView ......所以,我的代碼什麼也沒做,整個tableview保持在44的默認高度。這就是爲什麼我得到了44我的NSLog。
我會接受這個答案,但看到我原來的帖子上的編輯(我進一步深入後實現的東西)。 –
假設設置就緒,如果您返回您定義的值,但使用heightForRowAtIndexPath設置所有行的高度也將起作用,但效果不佳。 –