2015-10-16 57 views
-1

我有3行在我的表視圖。我做了一個自定義單元格。現在運行在不同的設備上,我想使表視圖的高度爲三個單元格可以適應它。在iPad上它工作正常,但在iPhone上它需要更多的底部空間,所以請告訴我如何獲得每個設備的動態高度。如何根據ios中的行數設置表視圖的大小?

我正在使用以下方法。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    float main_height=self.table_view.frame.size.height; 
    float result_cell_height; 
    float percentage = (77.0/568.0); 
    float height = self.view.frame.size.height * percentage; 
    result_cell_height=(height>77.0)?height:77.0; 
    if(height>main_height/3) 
    { 
     NSLog(@"con true"); 
     result_cell_height=main_height/3; 
    } 
    NSLog(@"result height is %f",result_cell_height); 

    return result_cell_height; 
} 

在此先感謝。

+0

該方法不會改變tableview高度!這是行高!!!!我假設你正在嘗試改變單元格高度,以便tableview將有3個單元格適合它!是對的嗎 ? –

+0

集合的行高會改變表格的高度我猜 – Techiee

+0

看到我的擔心是因爲表格的高度應該是三個單元格的高度或每個單元格的高度應該一樣大,所以三個單元格可以放入表格視圖 – Techiee

回答

0
float myHeight = self.view.frame.size.height * (77/568); 
[self.table_view setFrame:(CGRect){0, 0, self.view.frame.size.width, myHeight}]; 

你說你想讓表格調整爲不同的設備,所以這樣做。你也說你希望它完全適合3細胞,使:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    float myHeight = self.view.frame.size.height * (77/568); 
    return myHeight/3; 
} 

相反,如果你想要的細胞是一致的大小和表的大小是基於對(我推薦這個),則:

#define ROW_HEIGHT 40.0f 

... 

self.table_view setFrame:(CGRect){0, 0, self.view.frame.size.width, ROW_HEIGHT * 3}]; 

... 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return ROW_HEIGHT; 
} 
相關問題