【问题标题】:table cell height issue iOS?表格单元格高度问题iOS?
【发布时间】:2016-04-23 09:07:24
【问题描述】:

我有自定义表格视图。我在其中添加了 4 个视图

1.ImageView 2.三个标签

现在我希望该图像应该为多个设备增加尺寸。为此,我已将其设置在约束之下。可选高度为 176:339

现在为了增加图像的高度,我增加了我在代码下方使用的表格的高度。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    if(!self.customCell)
    {
        self.customCell = [self.tableview dequeueReusableCellWithIdentifier:@"tableCell"];
    }
    CGFloat height;
    height=self.customCell.img_main.frame.size.height;
    height=height+self.customCell.label_one.frame.size.height;
    height=height+self.customCell.label_two.frame.size.height;
    height=height+self.customCell.label_three.frame.size.height;
    height=height+self.customCell.dev_name.frame.size.height;
    NSLog(@"height is %f",self.customCell.img_main.frame.size.height);

    return height;
}

图像高度始终为 176,这是我在界面生成器中为 4 英寸屏幕设置的。

为什么图像和表格视图单元格的高度都没有增加?

【问题讨论】:

  • 因为在你访问框架属性的那一刻,这些视图还没有布局,它的框架还没有更新。我建议你设置 self.tableView.rowHeight = UITableViewAutomaticDimension 而不是覆盖 heightForRowAtIndexPath: 方法。这会自动为你计算每个单元格的行高。
  • 单元格高度和屏幕高度的比例是多少?比如80 : 100?,如果你知道比例应该是多少,你可以像[UIScreen mainScreen].bounds.size.height * 0.8一样返回单元格高度。并且会自动计算子视图的高度。
  • 是的,它有效。如果我想根据内容而不是根据纵横比增加高度怎么办
  • @arturdev self.tableView.rowHeight = UITableViewAutomaticDimension 对我不起作用

标签: ios objective-c iphone uitableview


【解决方案1】:

这是你的第二个问题,如果你想根据它显示的图像更改图像视图的高度,我建议使用高度约束而不是比例高度约束,计算会更容易。当你得到图像,并且知道图像大小,所以你计算并得到图像视图的大小,然后更新高度约束。完成。

将约束拖到您的单元格

设置图像时更新约束

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CustomeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellId" forIndexPath:indexPath];
    cell.image = image;
    cell.imageViewHeightConstraint.constant = [self calcuteSizeWithImage:image]; //you need to do the calculation yourself
    return cell;
}

【讨论】:

  • 我根据什么计算图像大小?
  • 图片大小还是图片视图大小?如果您的意思是图像视图,那取决于您要如何将视图调整为其内容。例如,如果您希望图像视图具有其图像的纵横比并用其图像填充图像视图,则图像视图的高度应为 imageViewWidth * imageHeight / imageWidth
猜你喜欢
  • 1970-01-01
  • 2011-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-09
  • 1970-01-01
  • 2017-07-19
  • 1970-01-01
相关资源
最近更新 更多