更改常量视图后,自动布局无法正常工作?

时间:2015-01-14 12:06:13

标签: ios autolayout constants

错误信息:

2015-01-14 19:10:05.165 mykey_family[14008:4218895] collection:section=4,count=0,newheight=0.000000
2015-01-14 19:10:05.166 mykey_family[14008:4218895] oldHeight=142.500000
2015-01-14 19:10:05.167 mykey_family[14008:4218895] TEST:section = 4, height = 177.000000 (this print in - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath)
2015-01-14 19:10:05.176 mykey_family[14008:4218895] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x7f8a4e04f3b0 V:|-(10)-[UIImageView:0x7f8a4e049190]   (Names: '|':UITableViewCellContentView:0x7f8a4e048320 )>",
    "<NSLayoutConstraint:0x7f8a4e04f430 V:[UIImageView:0x7f8a4e049190(40)]>",
    "<NSLayoutConstraint:0x7f8a4e051390 V:[UIImageView:0x7f8a4e049190]-(7)-[UILabel:0x7f8a4e04a950'\U6c5f\U82cf\U53ef\U4e00\U4e0a\U5e02\U4e86\Uff01\U6c5f\U82cf\U53ef\U4e00\U4e0a\U5e02\U4e86\Uff01\U6c5f\U82cf\U53ef\U4e00\U4e0a\U5e02\U4e86\Uff01\U6c5f...']>",
    "<NSLayoutConstraint:0x7f8a4e051850 V:[UILabel:0x7f8a4e04a950'\U6c5f\U82cf\U53ef\U4e00\U4e0a\U5e02\U4e86\Uff01\U6c5f\U82cf\U53ef\U4e00\U4e0a\U5e02\U4e86\Uff01\U6c5f\U82cf\U53ef\U4e00\U4e0a\U5e02\U4e86\Uff01\U6c5f...']-(5)-[UILabel:0x7f8a4e04adf0'\U5c55\U5f00\U5168\U6587']>",
    "<NSLayoutConstraint:0x7f8a4e051990 V:[UILabel:0x7f8a4e04adf0'\U5c55\U5f00\U5168\U6587'(0)]>",
    "<NSLayoutConstraint:0x7f8a4bdbeee0 V:[UILabel:0x7f8a4e04adf0'\U5c55\U5f00\U5168\U6587']-(5)-[UICollectionView:0x7f8a4c0eba00]>",
    "<NSLayoutConstraint:0x7f8a4e050e90 V:[UICollectionView:0x7f8a4c0eba00(142.5)]>",
    "<NSLayoutConstraint:0x7f8a4e053f00 V:[UICollectionView:0x7f8a4c0eba00]-(8)-[UILabel:0x7f8a4e04d860'55']>",
    "<NSLayoutConstraint:0x7f8a4e053f50 V:[UILabel:0x7f8a4e04d860'55']-(12)-|   (Names: '|':UITableViewCellContentView:0x7f8a4e048320 )>",
    "<NSLayoutConstraint:0x7f8a4bc88900 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7f8a4e048320(177)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7f8a4e053f00 V:[UICollectionView:0x7f8a4c0eba00]-(8)-[UILabel:0x7f8a4e04d860'55']>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

2015-01-14 19:10:05.183 mykey_family[14008:4218895] collection:section=4,count=0,newheight=0.000000
2015-01-14 19:10:05.183 mykey_family[14008:4218895] oldHeight=142.500000

控制器代码:

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger section = [indexPath section];
UITableViewCell *cell = nil;

if (section > [self.classZoneInfo count]) {
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:CellIdentifier];
    }
}
else{
    ClassZone *info = [self.classZoneInfo objectAtIndex:section];

    if (nil != info) {
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierTextAndImg];
        if (cell == nil)
        {
            cell = [[ClassZoneFullCell alloc] initWithStyle:UITableViewCellStyleDefault
                                            reuseIdentifier:CellIdentifierTextAndImg];
        }

        ClassZoneFullCell *tempCell = (ClassZoneFullCell *)cell;
        [tempCell initCell:info];
    }
    else{
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                          reuseIdentifier:CellIdentifier];
        }
    }
}

cell.backgroundColor = [UIColor clearColor];
[cell.contentView setBackgroundColor:[UIColor clearColor]];

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

return cell;
}

ClassZoneFullCell.m代码

-(void)updateConstraints
{
    [super updateConstraints];

    CGRect windowRect = [[UIScreen mainScreen] bounds];
    CGFloat width = (windowRect.size.width - (IMG_INNER_WIDTH+IMG_OUTER_WIDTH)*2 - TWO_IMG_SPACE*(MAX_IMG_OF_LINE-1))/4.0f;

    if (nil != self.classZone) {

        if (self.classZone.imgCount > 4) {
            width = (width+TWO_IMG_SPACE/2)*2;
        }
        else if(0 == self.classZone.imgCount){
            width = 0;
        }

        NSLog(@"collection:section=%ld,count=%ld,newheight=%f",(long)self.classZone.section,self.classZone.imgCount,width);

        for (NSLayoutConstraint *c in self.collectionConstraints) {
            NSLog(@"oldHeight=%f",c.constant);
            c.constant = width;
        }

        if (self.classZone.more.length > 2) {
            if (CLOSE_LINK == self.textType) {
                [self.context setNumberOfLines:3];
            }
            else if (OPEN_LINK == self.textType) {
                [self.context setNumberOfLines:0];
            }
            width = 20.0f;
        }
        else{
            width = 0.0f;
        }


        for (NSLayoutConstraint *c in self.moreTextConstraints) {
            c.constant = width;
        }
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    [self.contentView setNeedsLayout];
    [self.contentView layoutIfNeeded];

    self.context.preferredMaxLayoutWidth = CGRectGetWidth(self.context.frame);
}

- (void)initCell:(ClassZone *)info {
    self.classZone = info;

    self.timeTitle.text = info.timeTitle;
    self.teacher.text = info.teacher;
    self.context.text = info.context;
    self.moreText.text = info.more;
    self.favorCount.text = info.favorCount;
    self.commentCount.text = info.commentCount;
    self.classZoneImg.tag = info.section;
    self.moreText.tag = info.section;
    self.textType = info.linkType;

    [self setNeedsUpdateConstraints];
    [self setNeedsLayout];
}

我的问题:

在updateConstraints()函数中:我改变了一个视图常量(142.5-> 0),根据异常打印约束是使用旧值,而不是新值(0.0f),所以导致异常,如何解决问题?谢谢!

0 个答案:

没有答案