UITableViewCell 约束在 iOS 8 中看不到.我该如何缓解这种情况? [英] UITableViewCell constraints not seen in iOS 8. How do I mitigate this?

查看:15
本文介绍了UITableViewCell 约束在 iOS 8 中看不到.我该如何缓解这种情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解为什么我在 Storyboard 上为我的单元格中的不同标签设置的约束没有完成.这很重要,因为我的身高是动态的.

I'm trying to understand why the constraints I have put in place on the Storyboard for my different labels in my cell aren't being done. This is important because my height is dynamic.

过去 2 天我一直遇到这个问题,这让我很恼火.不,UITableViewAutomaticDimension 不是我想要的方式.

I have had this problem for the past 2 days and it's driving me up the wall. No, UITableViewAutomaticDimension is not how I want to do this.

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

    var cell = self.feed.dequeueReusableCellWithIdentifier("post") as PostCell

    var post = self.posts[indexPath.row]

    cell.name.text = post.name
    cell.timestamp.text = post.timestamp
    cell.postBody.text = post.body

    println("\(cell.name.constraints())")
    println("\(cell.postBody.constraints())")

    cell.contentView.setTranslatesAutoresizingMaskIntoConstraints(false)
    cell.setNeedsUpdateConstraints()     
    cell.updateConstraintsIfNeeded()

    cell.bounds = CGRectMake(0.0, 0.0, CGRectGetWidth(feed.bounds), CGRectGetHeight(cell.bounds))

    cell.setNeedsLayout()
    cell.layoutIfNeeded()

    var height = cell.contentView.systemLayoutSizeFittingSize(UILayoutFittingExpandedSize).height

    height += 1.0

    return height
}

当我打印为 cell.postBody 设置的约束时,我得到 [].但是,我有 5 个限制条件.Superview 的尾随空间,Superview 的前导空间,Superview 的底部空间等于 4,2 个顶部空间到 2 个不同的标签等于 8.

When I print the constraints set for cell.postBody I get []. However, I have 5 constraints. A trailing space to Superview, leading space to Superview, Bottom space to Superview Equals 4, and 2 Top Spaces to 2 different labels Equals 8.

如果我的代码无法通过 Storyboard 查看约束,我该如何以编程方式设置这 5 个约束

If it isn't possible for my code to see the constraints via the Storyboard, how do I programmatically set these 5 constraints

更新了我的做法:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell = self.feed.dequeueReusableCellWithIdentifier("post") as PostCell

    var post = self.posts[indexPath.row]

    cell.name.text = post.name
    cell.timestamp.text = post.timestamp
    cell.postBody.text = post.body

    if cachedHeights[post.id] == nil && cell.bounds.height != 0.0 {
        cachedHeights[post.id] = cell.bounds.height
    }

    return cell
}

func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

    var post = posts[indexPath.row]

    if cachedHeights[post.id] != nil {
        return cachedHeights[post.id]!
    } else {
        return 70
    }
}

问题是,我不确定 cell.bounds.height 是否完全准确.我的意思是,我认为它有时会使用前一个大单元格的高度(可能来自它为新单元出列的单元格.)

The problem is, I'm not sure cell.bounds.height is completely accurate. In that I mean, I think it is using the height of a previous large cell sometimes (perhaps from the cell it dequeued for the new one.)

推荐答案

约束不在标签上.这就是为什么他们的 constraints 属性返回一个空数组.

The constraints aren't on the labels. That's why their constraints property returns an empty array.

您将在标签的超级视图 cell.contentsView 上找到约束.

You'll find the constraints on the labels' superview, cell.contentsView.

您可以跳过对 setNeedsUpdateConstraintsupdateConstraintsIfNeeded 的调用,因为 layoutSubviews 将调用 updateConstraintsIfNeeded.

You can skip the calls to setNeedsUpdateConstraints and updateConstraintsIfNeeded as layoutSubviews will call updateConstraintsIfNeeded.

我在切换到自动调整单元格大小之前使用的旧方法:

我没有任何 swift 代码,但在我切换到自动调整大小的单元格之前,这里有一些来自应用程序的旧代码.它(隐式)使用放置在情节提要单元中标签上的约束.我所做的唯一不同的是缓存大小调整单元.

I don't have any swift code, but here's some old code from an app before I switched over to self-sizing cells. It (implicitly) uses the constraints placed on the labels in the storyboard cell. The only thing different I did is caching the sizing cell.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static LeftDetailTableViewCell *cell;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    });

    // configure the cell for this indexPath

    [self configureCell:cell atIndexPath:indexPath];

    // Set the sizing cell's width to the tableview's width

    cell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(tableView.bounds), CGRectGetHeight(cell.bounds));

    [cell setNeedsLayout];
    [cell layoutIfNeeded];

    // get the fitting size

    CGSize s = [cell.contentView systemLayoutSizeFittingSize: UILayoutFittingCompressedSize];
    return s.height + 1.0;
}

更新:

这是我现在使用的代码,用于 iOS 8 中的自定大小单元.

Here's the code I'm using now, for self-sized cells in iOS 8.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BIBLETableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    [cell adjustSizeToMatchWidth:CGRectGetWidth(self.tableView.frame)];

    [self configureAccessoryTypeForCell:cell forRowAtIndexPath:indexPath];

    [cell adjustConstraintsToMatchSeparatorInset:self.tableView.separatorInset];

    [self configureCell:cell forRowAtIndexPath:indexPath];

    [cell setNeedsUpdateConstraints];
    [cell updateConstraintsIfNeeded];

    return cell;
}

我的子类单元格有:

- (void)adjustSizeToMatchWidth:(CGFloat)width
{
    // Workaround for visible cells not laid out properly since their layout was
    // based on a different (initial) width from the tableView.

    CGRect rect = self.frame;
    rect.size.width = width;
    self.frame = rect;

    // Workaround for initial cell height less than auto layout required height.

    rect = self.contentView.bounds;
    rect.size.height = 99999.0;
    rect.size.width = 99999.0;
    self.contentView.bounds = rect;
}

- (void)adjustConstraintsToMatchSeparatorInset:(UIEdgeInsets)inset
{
    if (self.leadingMargins)
    {
        for (NSLayoutConstraint *constraint in self.leadingMargins)
        {
            constraint.constant = inset.left;
        }
    }

    if (self.trailingMargins)
    {
        for (NSLayoutConstraint *constraint in self.trailingMargins)
        {
            constraint.constant = self.accessoryType == UITableViewCellAccessoryDisclosureIndicator ? 0.0f : inset.left;
        }
    }
}

这篇关于UITableViewCell 约束在 iOS 8 中看不到.我该如何缓解这种情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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