2015-10-16 53 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; 
} 
相关问题