自定义编程的UITableViewCell的自动布局在滚动失败 [英] Auto layout of custom programmatic UITableViewCell fails upon scrolling

查看:117
本文介绍了自定义编程的UITableViewCell的自动布局在滚动失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用的iOS 6的新的自动排版功能在一个自定义的UITableViewCell已编程实现。我加了addConstraint调用,它在序曲一正常工作,直到我滚动。当我回来到小区滚动的布局后丢弃。通过丢弃我的意思是字段之间的利润都是错误的(过大,远远超出了细胞的大小)。我猜测这有什么做的dequeueReusableCellWithIdentifier方法留下我一个脏单元格,你发现自己需要重新初始化细胞中的字段相同的方式,但我似乎无法做任何事情来哄它来正确地呈现再次。我试图返回小区之前调用[self.contentView updateConstraints。我已经试过破坏的约束和重建他们。它不仅不能正常工作,但如果它试图在layoutSubviews它在某种无限循环冻结。任何想法?

这里的code建立的制约因素。它位于initWithStyle:reuseIdentifier:

  [self.completedLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.nextSetHeaderLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.nextSetDetailLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.youWillLearnHeaderLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.youWillLearnDetailLabel setTranslatesAutoresizingMaskIntoConstraints:NO];[self.contentView removeConstraints:[self.contentView约束]];*的NSDictionary意见= NSDictionaryOfVariableBindings(_completedLabel,_nextSetHeaderLabel,_nextSetDetailLabel,_youWillLearnHeaderLabel,_youWillLearnDetailLabel);[self.contentView addConstraints:
 [NSLayoutConstraint constraintsWithVisualFormat:@H:| -5 - [_ completedLabel] -5 |
                                         选项​​:0
                                         指标:无
                                           观点:观点]];
[self.contentView addConstraints:
 [NSLayoutConstraint constraintsWithVisualFormat:@H:| -5 - [_ nextSetHeaderLabel] -5 |
                                         选项​​:0
                                         指标:无
                                           观点:观点]];[self.contentView addConstraints:
 [NSLayoutConstraint constraintsWithVisualFormat:@H:| -5 - [_ nextSetDetailLabel] -5 |
                                         选项​​:0
                                         指标:无
                                           观点:观点]];[self.contentView addConstraints:
 [NSLayoutConstraint constraintsWithVisualFormat:@H:| -5 - [_ youWillLearnHeaderLabel] -5 |
                                         选项​​:0
                                         指标:无
                                           观点:观点]];[self.contentView addConstraints:
 [NSLayoutConstraint constraintsWithVisualFormat:@H:| -5 - [_ youWillLearnDetailLabel] -4- |
                                         选项​​:0
                                         指标:无
                                           观点:观点]];[self.contentView addConstraints:
 [NSLayoutConstraint constraintsWithVisualFormat:@\"V:|-5-[_completedLabel]-12-[_nextSetHeaderLabel]-0-[_nextSetDetailLabel]-12-[_youWillLearnHeaderLabel]-0-[_youWillLearnDetailLabel(>=20)]-1-|\"
                                         选项​​:0
                                         指标:无
                                           观点:观点]];


解决方案

我就遇到了这个问题为好。如果我没有出列单元格,一切似乎工作 - 滚动,旋转等。但是,如果我出列单元格,然后布局开始变得混乱。我能得到它的工作的唯一方法是通过覆盖小区的prepareForReuse方法。在该方法中,


    1.去除所有的自定义子视图结果
    2.删除从内容查看结果的子视图相关联的所有限制
    3.再次添加子视图和约束结果

   - (无效)prepareForReuse
{
    [个体经营removeCustomSubviewsFromContentView]
    [self.contentView removeConstraints:self.constraints] //self.constraits保存所有添加的约束
    [个体经营setupSubviewsInContentView]
    [个体经营addConstraintsToContentView]
}

如果有更好的方式来做到这一点,我很愿意去学习,以及:)我相信dequeing的优点是的tableView不必保留在内存中大量的细胞 - 但是,用这种方法一要经过基本上建立你出列的单元格,每次的费用。

I'm trying to use the new auto layout capability of iOS 6 on a custom UITableViewCell which has been implemented programmatically. I added the addConstraint calls, and it works properly at first-- until I scroll. When I come back to the cell after scrolling the layout is trashed. By trashed I mean the margins between fields are all wrong (too large, well beyond the size of the cell). I'm speculating this has something to do with the dequeueReusableCellWithIdentifier method leaving me with a "dirty" cell, the same way you find yourself needing to reinitialize fields within cells, but I can't seem to do anything to coax it to render properly again. I've tried calling [self.contentView updateConstraints] before returning the cell. I've tried destroying the constraints and recreating them. Not only does it not work, but if it's attempted in layoutSubviews it freezes in an endless loop of some kind. Any ideas?

Here's the code to establish the constraints. It's located in initWithStyle:reuseIdentifier:

[self.completedLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.nextSetHeaderLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.nextSetDetailLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.youWillLearnHeaderLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.youWillLearnDetailLabel setTranslatesAutoresizingMaskIntoConstraints:NO];

[self.contentView removeConstraints:[self.contentView constraints]];

NSDictionary *views = NSDictionaryOfVariableBindings(_completedLabel, _nextSetHeaderLabel, _nextSetDetailLabel, _youWillLearnHeaderLabel, _youWillLearnDetailLabel);

[self.contentView addConstraints:
 [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[_completedLabel]-5-|"
                                         options:0
                                         metrics:nil
                                           views:views]];
[self.contentView addConstraints:
 [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[_nextSetHeaderLabel]-5-|"
                                         options:0
                                         metrics:nil
                                           views:views]];

[self.contentView addConstraints:
 [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[_nextSetDetailLabel]-5-|"
                                         options:0
                                         metrics:nil
                                           views:views]];

[self.contentView addConstraints:
 [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[_youWillLearnHeaderLabel]-5-|"
                                         options:0
                                         metrics:nil
                                           views:views]];

[self.contentView addConstraints:
 [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[_youWillLearnDetailLabel]-4-|"
                                         options:0
                                         metrics:nil
                                           views:views]];

[self.contentView addConstraints:
 [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-5-[_completedLabel]-12-[_nextSetHeaderLabel]-0-[_nextSetDetailLabel]-12-[_youWillLearnHeaderLabel]-0-[_youWillLearnDetailLabel(>=20)]-1-|"
                                         options:0
                                         metrics:nil
                                           views:views]];

解决方案

I ran into this issue as well. If I wasn't dequeuing cells, everything seemed to work - scrolling, rotation etc. However, if I dequeued cells, then the layout started getting messed up. The only way I could get it to work was by overriding the cell's prepareForReuse method. In this method,

    1. remove all the custom subviews
    2. remove all constraints associated with those subviews from contentView
    3. add subviews and constraints again

-(void) prepareForReuse
{
    [self removeCustomSubviewsFromContentView];
    [self.contentView removeConstraints:self.constraints] //self.constraits holds all the added constraints
    [self setupSubviewsInContentView];
    [self addConstraintsToContentView];
}

If there is a better way to do this, I would love to learn as well :) I believe the advantage of dequeing is that the tableView does not have to hold a large number of cells in memory - but, with this method, one has to go through the cost of essentially setting up the cell everytime you dequeue.

这篇关于自定义编程的UITableViewCell的自动布局在滚动失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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