我动态地添加一个UIImageView
(作为缩略图)及其NSLayoutConstraints
在我的UIView
它在一个表的单元格中。我有两个关于这个问题。UIImageView重复显示在另一个单元格
添加图片视图后,如果用户在此表中插入文字仍然显示图片。我的意思是,而不是文本表格打印更多的图像。但是,如果我停止并重新运行应用程序,则此时显示的文本应该如此。不过,如果我写一个文字图片即将到来。为什么,我该怎么做?
我设定图像和它的约束是这样的:
-(void)setThumbnail:(UIImageView)imageView { [self.messageView addSubview:imageView]; NSLayoutConstraint *leadingOfThumbnail = [NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.messageView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:8.f]; NSLayoutConstraint *trailingOfThumbnail = [NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.messageView attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:-8.f]; NSLayoutConstraint *topOfThumbnail = [NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.messageView attribute:NSLayoutAttributeTop multiplier:1.0 constant:8.f]; NSLayoutConstraint *bottomOfThumbnail = [NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.messageView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-8.f]; [self.messageView addConstraint:leadingOfThumbnail]; [self.messageView addConstraint:trailingOfThumbnail]; [self.messageView addConstraint:topOfThumbnail]; [self.messageView addConstraint:bottomOfThumbnail]; [self.messageView setTranslatesAutoresizingMaskIntoConstraints:NO]; [self.messageView removeConstraints:[self.messageTextLabel constraints]]; }
并且在装载时,我得到一个约束错误。它说:
2016-10-07 09:35:32.532 application[3733:2922397] 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) (
"NSAutoresizingMaskLayoutConstraint:0x1281474d0 h=--& v=--& UIImageView:0x126d7e020.midY == + 100",
"NSAutoresizingMaskLayoutConstraint:0x128147520 h=--& v=--& V:[UIImageView:0x126d7e020(200)]",
"NSLayoutConstraint:0x126d6c880 V:|-(8)-[UIImageView:0x126d7e020] (Names: '|':UIView:0x1281480d0)")
Will attempt to recover by breaking constraint NSLayoutConstraint:0x126d6c880 V:|-(8)-[UIImageView:0x126d7e020] (Names: '|':UIView:0x1281480d0)
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.
我检查,由我设置的领先和顶部约束导致此错误。他们已经不能正确工作了。我的图像是200 * 200,因此它不需要高度和宽度限制。我只希望从消息视图获得8点领先,尾随,顶部,底部。问题是什么?
谢谢。
说明编辑第一个问题:表将随机放置UIImageView,多个单元格 - 不应该如此。
'[self.messageView addSubview:imageView];'这是罪魁祸首,因为单元格被重用。由于您似乎为您的单元格使用自定义.m,所以在'prepareForReuse'中,从'self.messageView'中删除所有子视图,那里是UIImageView。 – Larme
@narsimelous:单元重用问题缓存动态添加到单元格的图像视图。写一些resetThumbnail {}方法来检查图像视图是否存在,然后将其删除并将单元约束重置回初始状态。重置它在每个prepareForReuse调用。 – kaushal