ios - UIImageView 重复显示在另一个单元格中

标签 ios objective-c uitableview uiview uiimageview

我在表格单元格中的 UIView 中动态添加了一个 UIImageView(作为缩略图)及其 NSLayoutConstraints。我有两个问题。

  1. 添加 ImageView 后,如果用户向该表中插入文本,仍会显示图像。我的意思是,而不是文本表打印一张图片。但是,如果我停止并重新运行该应用程序,这次文本会按应有的方式显示。不过,如果我写一个文字图片来了。为什么以及我应该做什么?

  2. 我这样设置图像及其约束:

    -(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 leading, trailing, top, bottom 从消息 View 。有什么问题?

谢谢。

第一个问题的解释编辑: 表格随机放置 UIImageView,多个单元格 - 不是它应该的样子。

最佳答案

您可能想要实现 prepareForReuse 方法并在那里做一些清理,将 imageView 设置为 nil 并将其从此处的 subview 中删除。
当您的单元格出列并即将在 tableview 中重复使用时,将调用此方法。

关于ios - UIImageView 重复显示在另一个单元格中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39910959/

相关文章:

javascript - 无法使用 document.cookie 在 UIWebview 浏览器中读取 cookie

ios - iOS 按文本宽度对 UITableView 进行排序

ios - JSQMessages 的自定义单元格

ios - 我应该为新的 ios 应用程序使用 "apple development"或 "ios distribution"证书吗?

objective-c - 从drawAtPoint绘制的屏幕中删除字符串

ios - 无法从阵列中获取正确的音频文件来播放

ios - 如何检查带小数的 double 是否可整除?

ios - 如何首先按文件夹、名称、大小、日期对 GTLQueryDrive 项目进行排序

ios - 核心数据跳过获取函数 Swift

ios - 将 indexPath.row 发送到另一个 UIViewController