【发布时间】:2016-10-07 06:41:07
【问题描述】:
我在表格单元格中的UIView 中动态添加UIImageView(作为缩略图)及其NSLayoutConstraints。我对此有两个问题。
添加图像视图后,如果用户在此表中插入文本,仍然会显示图像。我的意思是,代替文本表打印一个图像。但是,如果我停止并重新运行该应用程序,这次文本将按原样显示。不过,如果我写一个文字图片来了。为什么以及我应该怎么做?
-
我像这样设置图像及其约束:
-(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 pt 前导、尾随、顶部、底部。有什么问题?
谢谢。
第一个问题的解释编辑:表格随机放置 UIImageView,多个单元格 - 不是应该的。
【问题讨论】:
-
[self.messageView addSubview:imageView];这是罪魁祸首,因为单元格被重复使用。由于您似乎为您的单元格使用了自定义 .m,因此在prepareForReuse中,从self.messageView中删除所有属于 UIImageView 的子视图。 -
@narsimelous :单元格重用缓存动态添加到单元格的图像视图的问题。编写一些 resetThumbnail {} 方法来检查图像视图是否存在,然后将其删除并将单元格约束重置回初始状态。在每次 prepareForReuse 调用时重置它。
标签: ios objective-c uitableview uiview uiimageview