Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
表视图iOS8中的单元格自动布局_Ios_Objective C_Uitableview_Autolayout - Fatal编程技术网

表视图iOS8中的单元格自动布局

表视图iOS8中的单元格自动布局,ios,objective-c,uitableview,autolayout,Ios,Objective C,Uitableview,Autolayout,我的表格视图单元格似乎无法使用自动布局。 在某些细胞上它似乎起作用,而在另一些细胞上它似乎不起作用。甚至是完全相同类型的细胞 例如,在某些单元格上,说明将超过1行文字,并且将正常工作 …但在其他单元格上,描述将超过1行文字,但仅显示1行文字,其中有一堆空白 你能帮我找出我遗漏了什么或做错了什么吗?谢谢 我用这个StackOverflow问题来指导我的流程,作为第一次这样做的人: 1。设置和添加约束 我相信这些在很大程度上都很有效 2。确定唯一的表视图单元重用标识符 我不完全确定我是否需要担心这一

我的表格视图单元格似乎无法使用自动布局。

在某些细胞上它似乎起作用,而在另一些细胞上它似乎不起作用。甚至是完全相同类型的细胞

例如,在某些单元格上,说明将超过1行文字,并且将正常工作

…但在其他单元格上,描述将超过1行文字,但仅显示1行文字,其中有一堆空白

你能帮我找出我遗漏了什么或做错了什么吗?谢谢

我用这个StackOverflow问题来指导我的流程,作为第一次这样做的人:

1。设置和添加约束

我相信这些在很大程度上都很有效

2。确定唯一的表视图单元重用标识符

我不完全确定我是否需要担心这一部分,因为我总是有标题、时间和描述

适用于iOS 8-自动调整单元格大小 3.启用行高估计

我将此添加到
viewDidLoad

self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 180.0;
更新:根据Acey请求添加更多信息

为了清楚起见,我提出了一些限制条件:

  • 标题:左15,上85,右15
  • 标题和时间之间的垂直间距为10
  • 时间和描述之间的垂直间距为10
  • 我单击了所有三个标签并添加了前缘和尾部 边缘
  • 我在“描述”和“表格视图”单元格底部之间固定了20个
更新2:已解决 下面的答案非常有效,但任何额外的间距都是由于为单元格设置的高度太大,所以Xcode会自动添加额外的空间来填充单元格的高度,因为文本标签并没有填充表格视图单元格的整个高度

如果你有任何问题或需要任何帮助,请告诉我,如果你遇到这个问题,并有相同的问题


谢谢大家

我还没有尝试使用新的iOS 8机制。但我在使用iOS 6/7进行此操作时也遇到过类似的问题。在将应用程序升级到iOS 8之后,它仍然可以正常工作,所以也许旧的方式仍然是最好的方式

我这里有一些代码示例:

在这里:

长话短说iOS 8之前的方法是保留一个单元格副本,仅用于计算内部高度
tableView:heightforrowatinexpath:
。但这还不足以处理多行
UILabel
。每次调用
layoutSubviews
时,我都必须对
UILabel
进行子类化,以更新
preferredMaxLayoutWidth

preferredMaxLayoutWidth
“修复”似乎是我遗漏的神奇秘密。一旦我这样做了,我的大部分细胞都能正常工作

第二个问题我只要求我正确设置内容压缩阻力和内容拥抱属性,例如,告诉标签拥抱文本将意味着它不会扩展以填充空白,这将导致单元格收缩

一旦我做了这两件事,我的单元格现在可以处理任意字体大小或任意数量的文本,而不需要任何杂乱的布局代码。要学的东西很多,但我确实认为最终还是有回报的,因为我的应用程序中有很多动态内容

编辑

在iOS8上遇到了一些我自己的问题之后,我添加了一些细节来解决这些非常奇怪的自动布局错误

对于我提到的代码,当单元格“行高”未设置为自定义时,它似乎不起作用。通过选择单元格并单击autoLayout选项卡(其中包含所有内容压缩阻力设置等),可以在IB中找到此设置。按下复选框,它将填充一个临时高度

其次,在我的代码中,我保留了一个单元格的本地副本,然后在
heightforrowatinexpath:
方法中多次重用它。这似乎会在每次调用时将单元格高度增加很多。我必须通过调用以下命令重新初始化本地副本:

localCopy = [self.tableView dequeueReusableCellWithIdentifier:@"mycell"];
新的Xcode 6/iOS 8更改似乎与iOS 7非常不向后兼容,而且管理方式似乎完全不同

希望这有帮助

编辑2

看完这个问题:

我遇到了iOS 7/iOS 8自动布局支持的另一个问题!!!我正在覆盖iOS 8的
layoutSubviews
,调用super后,我还需要覆盖
setBounds
来更新
preferredMaxLayoutWidth
。WTF改变了苹果

IB中的
preferredMaxLayoutWidth
设置似乎有问题,因为iOS 7无法使用自动功能,如果在多个设备上使用相同的
UILabel
,它只会使用1宽度。因此,iOS 8平板电脑上的UITableViewCell将更大,因为在iOS 8 iPhone上,同一个手机需要两行。

以下是我的尝试

您可以创建一个方法/函数,以获得所需的cellview。像这样:

- (UIView *) getCellView {

    UIView *cellView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.view.frame.size.width, 0.0f)];
    cellView.tag = 1;
    cellView.backgroundColor = [UIColor clearColor];

    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(15.0f, 10.0f, 15.0f, 15.0f)]; //I assumed that was the size of your imageView;
    imgView.image = [UIImage imageNamed:@"whatever-your-image-is-called"];
    [cellView addSubview:imgView];

    CGFloat xPadding = 15.0f;
    CGFloat yPadding = 15.0f;
    UILabel *headlineLabel = [[UILabel alloc ]initWithFrame:CGRectMake(xPadding, 0.0f, self.view.frame.size.width - (xPadding*2), 0.0f)];
    headlineLabel.numberOfLines = 0;
    headlineLabel.text = @"Red Sox season fell apart after World Series title (The Associated Press)";
    [headlineLabel sizeToFit];
    CGRect hFrame = headlineLabel.frame;
    if(hFrame.size.width > self.view.frame.size.width - 31.0f)
        hFrame.size.width = self.view.frame.size.width - 30.0f;
    hFrame.origin.y = imgView.frame.size.height + yPadding;
    headlineLabel.frame = hFrame;

    UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(xPadding, 0.0f, self.view.frame.size.height-(xPadding*2), 0.0f)];
    timeLabel.text = @"4h";
    //timeLabel.numberOfLines = 0; //uncomment if it will wrap on multiple lines;
    [timeLabel sizeToFit];
    hFrame = timeLabel.frame;
    if(hFrame.size.width > self.view.frame.size.width - 31.0f)
        hFrame.size.width = self.view.frame.size.width - 30.0f;
    hFrame.origin.y = headlineLabel.frame.size.height + yPadding;
    timeLabel.frame = hFrame;

    UILabel *descriptLabel = [[UILabel alloc] initWithFrame:CGRectMake(xPadding, 0.0f, self.view.frame.size.height - (xPadding*2), 0.0f)];
    descriptLabel.text = @"Boston (AP) -- To Boston Red Sox manager John Farrel, it hardly seems possible that just 11 months ago his team was celebrating the World Series championship on the field at Fenway Park.";
    descriptLabel.numberOfLines = 0; //I would suggest something like 4 or 5 if the description string vary from 1 line to more than 5 lines.
    [descriptLabel sizeToFit];
    hFrame = descriptLabel.frame;
    if(hFrame.size.width > self.view.frame.size.width - 31.0f)
        hFrame.size.width = self.view.frame.size.width - 30.0f;
    hFrame.origin.y = timeLabel.frame.size.height + yPadding;
    descriptLabel.frame = hFrame;

    cellView.frame = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, descriptLabel.frame.origin.y + descriptLabel.frame.size.height + 15.0f /*some padding*/);
    return cellView;
}
如果您使用的是indexPath.row,那么只需将方法名称更改为
-(UIView*)getCellView:(NSIndex)*indexPath
,它的工作原理应该是一样的

那么在你的高度上你可以做什么

return[[self-getCellView]frame].size.height

return[[self-getCellView:indexPath]frame].size.height

在cellForRowAtIndexPath中,您可以执行以下操作

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.textLabel.text = @"";
    cell.detailTextLabel.text = @"";
}
[[cell viewWithTag:1] removeFromSuperview];
[cell addSubview:[self getCellView]; //or [cell addSubview:[self getCellView:indexPath]];
return cell;

希望这有帮助。如果有什么不清楚或不太正常,请告诉我。有一些东西你可能需要调整以适应你的使用,特别是在cellForRowAtIndexPath中,但这应该是你需要做的一切。愉快的编码。

如果您试图只针对iOS 8及以上版本,并且能够正确地设置约束条件,则可以省略
tableView:heightforrowatinexpath:
方法。看起来您可能与creat有问题