自定义 UITableviewcell 高度设置不正确 [英] custom UITableviewcell height not set correctly

查看:40
本文介绍了自定义 UITableviewcell 高度设置不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我探索了关于这个问题的现有问答,但没有找到我的答案.

I explored the existing Q&As on SO about this question but did not find my answer.

我知道这是由 tableview 在运行时不知道自定义单元格的高度引起的,但不知道如何解决这个问题.这是 iOS 8 + Xcode 6.我为自定义单元格的内在大小做了所有自动布局所需的方法......

I know this is caused by tableview not knowing the height of the custom cell at run time,but not sure how to get over this. This is iOS 8 + Xcode 6. I did all the auto layout required methods for intrinsic size of the custom cell...

  • 带有标准单元格的标准表格视图,只有一个(行 = 2)在代码中创建为自定义单元格;

  • Standard tableview with standard cells, only one (row = 2) created in code as custom cell;

自定义单元格:

-(CGSize)intrinsicContentSize

{//用于测试目的的硬编码

{ //hardcode for testing purposes

return CGSizeMake(500.0, 450.0);

}

在iOS模拟器上运行时,发现customCell显示在其行下方其他单元格的下方",高度标准,与其他标准单元格高度设置相同,而不是其高度设置比其他单元格大很多单元格.

when running on iOS simulator, found that the customCell is displayed "underneath" other cells below its row, with height standard, the same way other standard cell height is set, instead of its height set much bigger than other cells.

在表视图控制器中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell;
    pageViewCellTableViewCell* customCell;

    if (indexPath.row != 2)
    {
       cell = [tableView dequeueReusableCellWithIdentifier:@"regularCell" forIndexPath:indexPath];


        cell.textLabel.text = [NSString stringWithFormat:@"Table Row %lu", indexPath.row];
        return cell;
    }
    else
    {
        customCell = [tableView dequeueReusableCellWithIdentifier:@"customCell"] ;

        if (customCell == nil) {

            customCell = [[customCell alloc] init];

        }

        customCell.parentViewController = self;


        [customCell  setNeedsUpdateConstraints];
        [customCell setNeedsLayout];
        [customCell setNeedsDisplay];


        return customCell;

    }

    return nil ;
}

- (CGFloat)tableView: (UITableView*)tableView EstimatedHeightForRowAtIndexPath: (NSIndexPath*) indexPath
 {
     if (indexPath.row != 2)
         return 10;
     else
         return 450;
 }

 - (CGFloat)tableView: (UITableView*)tableView HeightForRowAtIndexPath: (NSIndexPath*) indexPath
 {
     if (indexPath.row != 2)
         return 10;
     else
         return 450;
 }

在模拟器上运行时:

收到以下错误消息:仅警告一次:检测到约束模糊地建议 tableview 单元格的内容视图的高度为零的情况.我们正在考虑无意识的倒塌,而是使用标准高度.

Got the following Error msg: Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tableview cell's content view. We're considering the collapse unintentional and using standard height instead.

在此处输入代码">

推荐答案

你可以在 iOS 8 中使用 self-sizing cell(Swift 代码):

You can use self-sizing cells in iOS 8 (Swift code):

tableView.estimatedRowHeight = 88.0
tableView.rowHeight = UITableViewAutomaticDimension

下面列出了两个教程:

  1. 了解 iOS 8 中的自定大小单元格和动态类型
  2. iOS 8:自我调整表格视图具有动态类型的单元格

这篇关于自定义 UITableviewcell 高度设置不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆