如何根据屏幕尺寸设置 UITableViewCell 高度
How to set UITableViewCell height depending on screen size
我从网上下载资料,它由文字和图片组成。我将这些数据放入 UITableView,我希望图像成为 UITableViewCell 的背景。
所有图片都具有相同的分辨率 620x350。
所有的 iPhone 都有不同的屏幕尺寸,所以我需要根据屏幕尺寸缩放图像的宽度和高度,并对 UITableViewCell 执行此操作。
最好的解决方案是什么?
谢谢。
您可以使用 HTTableViewDelegate
中的 -tableView:heightForRowAtIndexPath:
方法来 return 每个单元格的高度。在你的情况下,你会想要这样的东西:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [UIScreen mainScreen].bounds.size.height;
}
只需使用- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
CGFloat aspectRatio = 350.0f/620.0f;
return aspectRatio * [UIScreen mainScreen].bounds.size.width;// assuming that the image will stretch across the width of the screen
}
最干净的解决方案,它只是 调整单元格的高度,让 图像由 Autolayout 自动调整大小.
很简单:
在您的单元格中,添加一个UIImageView
并设置当单元格具有实际高度时您想要的尺寸;
设置为这个UIImageView
的约束,至少top, bottom和其中一个横向边框(right or left);还设置约束以保持 比例 width/height;
此时,当单元格 增长时,图像的高度会增长 ,为了保持比例(感谢约束),它 调整了宽度,总是正确且成比例的大小。
因此,现在您只需编写单元格高度的代码即可:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
static CGFloat cellDefaultHeight = /*the default height of the cell*/;
static CGFloat screenDefaultHeight = /*the default height of the screen i.e. 480 in iPhone 4*/;
CGFloat factor = cellDefaultHeight/screenDefaultHeight
return factor * [UIScreen mainScreen].bounds.size.height;
}
显然,将UIImageView
的contentMode
设置为AspectFill
。
我从网上下载资料,它由文字和图片组成。我将这些数据放入 UITableView,我希望图像成为 UITableViewCell 的背景。 所有图片都具有相同的分辨率 620x350。
所有的 iPhone 都有不同的屏幕尺寸,所以我需要根据屏幕尺寸缩放图像的宽度和高度,并对 UITableViewCell 执行此操作。
最好的解决方案是什么?
谢谢。
您可以使用 HTTableViewDelegate
中的 -tableView:heightForRowAtIndexPath:
方法来 return 每个单元格的高度。在你的情况下,你会想要这样的东西:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [UIScreen mainScreen].bounds.size.height;
}
只需使用- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
CGFloat aspectRatio = 350.0f/620.0f;
return aspectRatio * [UIScreen mainScreen].bounds.size.width;// assuming that the image will stretch across the width of the screen
}
最干净的解决方案,它只是 调整单元格的高度,让 图像由 Autolayout 自动调整大小.
很简单:
在您的单元格中,添加一个
UIImageView
并设置当单元格具有实际高度时您想要的尺寸;设置为这个
UIImageView
的约束,至少top, bottom和其中一个横向边框(right or left);还设置约束以保持 比例 width/height;
此时,当单元格 增长时,图像的高度会增长 ,为了保持比例(感谢约束),它 调整了宽度,总是正确且成比例的大小。
因此,现在您只需编写单元格高度的代码即可:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
static CGFloat cellDefaultHeight = /*the default height of the cell*/;
static CGFloat screenDefaultHeight = /*the default height of the screen i.e. 480 in iPhone 4*/;
CGFloat factor = cellDefaultHeight/screenDefaultHeight
return factor * [UIScreen mainScreen].bounds.size.height;
}
显然,将UIImageView
的contentMode
设置为AspectFill
。