【问题标题】:Making a UITableViewCell resize to fit different screen sizes使 UITableViewCell 调整大小以适应不同的屏幕尺寸
【发布时间】:2016-03-31 11:08:36
【问题描述】:

我当前的问题是尝试为不同的 iOS 屏幕尺寸调整有限数量的单元格 [3]。我目前的原型单元格为 600x300,属于任意 x 任意大小级别,非常适合 iPhone 6。但是,当我尝试转到任何其他屏幕尺寸时,单元格要么太大,您必须滚动 [任何低于iPhone 6] 或不够大 [iPhone 6+、iPad 等]

单元格中都有一个 UIImageView 并在宽度上正确调整大小,但在高度上没有。

我只是在表格单元格中放了一些样机照片,以了解我的目标。我希望它到我不必滚动的地方,我相信你会在 TableViewController 中实现以下行:

tableView.scrollEnabled = false

【问题讨论】:

  • 尝试在Interface Builder中使用UIImageView模式,现在您似乎设置了Scale To Fill,这就是您的图像被拉伸的原因,请尝试设置纵横比匹配
  • 感谢您的回复,但是图像没有被拉伸。图像保持其大小,但无论硬件如何,单元格的高度仍继续为 300。我想根据您使用的设备将三个单元格完美地适合屏幕尺寸,但不知道该怎么做? @schmidt9

标签: ios uitableview


【解决方案1】:

这是一个代码 sn-p。关于图像大小,您基本上有两个关于UIImageView 的Mode 选项,请参见下面的屏幕截图。在 ImageTableViewCell 的 xib 中,您应该检查 Clip Subviews(如果您选择 Aspect Fill 选项,则需要)为您的 UIImageView 并在 ImageTableViewCell 中为其设置一个 Outlet myImageView。 ImageTableViewCell 只包含这个出口,在其 xib 中将图像视图的前导和尾随空间约束设置为相对于父视图为 0(使其拉伸)。

视图控制器

#import "SecondViewController.h"
#import "ImageTableViewCell.h"

#define kCellIdentifier @"cell"

@interface SecondViewController ()
{
    NSArray *_data;
}

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.scrollEnabled = NO;
    self.tableView.separatorColor = [UIColor clearColor];
    [self.tableView registerNib:[UINib nibWithNibName:@"ImageTableViewCell" bundle:nil] forCellReuseIdentifier:kCellIdentifier];
    _data = @[@"img.jpg", @"img.jpg", @"img.jpg"];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _data.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (self.tableView.bounds.size.height
            - self.navigationController.navigationBar.frame.size.height
            - [UIApplication sharedApplication].statusBarFrame.size.height) / 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    ImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier forIndexPath:indexPath];
    cell.myImageView.image = [UIImage imageNamed:_data[indexPath.row]];

    return cell;
}

@end

Aspect Fit

方面填充

【讨论】:

  • 这是一个了不起的解释,我能够采取点点滴滴的方式来完成我所需要的。谢谢
【解决方案2】:

主要的必要步骤是确定单元格的纵横比(宽度除以高度)和单元格中的图像。

假设它们相等(并假设纵横比为 16:9),请按以下步骤操作:

  1. 在viewDidLoad之后,根据纵横比计算行高来设置行高:

            let screenWidth             = CGRectGetWidth(UIScreen.mainScreen().bounds)
            let height                  = screenWidth * 9 / 16 
            self.tableView.rowHeight    = height
    
  2. 在原型单元格中,添加UIImageView。让它具有最大宽度(contentView 的前导和尾随空间 = 0),并将纵横比约束设置为 16:9(它不能有任何其他高度约束)。

【讨论】:

    【解决方案3】:

    我最终在@schmidt9 和@VinodVishwanath 的帖子的帮助下解决了这个问题。为了正确计算您希望内容所在的屏幕大小,您必须减去导航栏的空间以及状态栏(时间、电池等)。以下行可以执行此操作:

    要查找导航栏的大小:

    self.navigationController!.navigationBar.frame.size.height
    

    要查找包括导航栏和状态菜单在内的整个屏幕的大小:

    UIScreen.mainScreen().fixedCoordinateSpace.bounds.height
    

    所有设备的状态栏始终为 20,因此您可以简单地减去它。你终于得到了这段代码:

    let screenHeight = UIScreen.mainScreen().fixedCoordinateSpace.bounds.height - self.navigationController!.navigationBar.frame.size.height - 20
        self.tableView.rowHeight = screenHeight / 3
    

    把它更多地放在一个数学方程中: 总屏幕 - 导航栏 - 20(来自状态栏)= 总工作空间

    希望对您有所帮助,如有任何进一步的说明,请发表评论。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-23
      • 2017-11-11
      • 1970-01-01
      • 1970-01-01
      • 2016-06-16
      • 1970-01-01
      • 2015-11-18
      • 1970-01-01
      相关资源
      最近更新 更多