2016-10-07 94 views
0

我动态地添加一个UIImageView(作为缩略图)及其NSLayoutConstraints在我的UIView它在一个表的单元格中。我有两个关于这个问题。UIImageView重复显示在另一个单元格

  1. 添加图片视图后,如果用户在此表中插入文字仍然显示图片。我的意思是,而不是文本表格打印更多的图像。但是,如果我停止并重新运行应用程序,则此时显示的文本应该如此。不过,如果我写一个文字图片即将到来。为什么,我该怎么做?

  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点领先,尾随,顶部,底部。问题是什么?

谢谢。

说明编辑第一个问题:表将随机放置UIImageView,多个单元格 - 不应该如此。

+1

'[self.messageView addSubview:imageView];'这是罪魁祸首,因为单元格被重用。由于您似乎为您的单元格使用自定义.m,所以在'prepareForReuse'中,从'self.messageView'中删除所有子视图,那里是UIImageView。 – Larme

+0

@narsimelous:单元重用问题缓存动态添加到单元格的图像视图。写一些resetThumbnail {}方法来检查图像视图是否存在,然后将其删除并将单元约束重置回初始状态。重置它在每个prepareForReuse调用。 – kaushal

回答

1

对于第一个问题,您可以简单地设置单元格图像从tableview退出后为零。

对于第二个问题,ImageView的初始化后添加此行,以避免autoresizingmask被翻译为约束:

imageView.translatesAutoresizingMaskIntoConstraints = false 
+0

我尝试过,但没有初始化后。谢谢你的第二个问题!第一部分呢? – anyName

+0

我不明白你的第一个问题,你能否更好地解释它或者添加一个gif? – iGenio

0

我相信,你的问题是因为dequeCell功能。 UITbableView回收单元格。因此,如果您将图像放入一个单元格中,则可能会出现在另一个单元格中。

要解决这个问题,您需要在cellForRowAtIndexPath中放置一些条件。

这些条件将检查您是否想要当前单元格或图像或两者兼有的文本。

如果只需要文本,则需要从该单元中删除图像。

如果您仍然遇到问题,请分享您的cellForRowAtIndexPath代码。

+0

但我以编程方式添加图像。我应该怎么做? – anyName

+0

一种方法是将UIImageView放入您的自定义UITableViewCell类中。只有在需要图像时才初始化它。如果没有,将其设置为零,并将其从superView中删除。 另一种方法是迭代所有Cell子视图并检查UIImaqeViewClass。删除该子视图。虽然我宁愿第一种方式 –

+0

是有道理的。谢谢! 我不希望它成为IBOutlet。那不会导致我认为的问题? @Shayan Jalil – anyName

1

您可能想要实现prepareForReuse方法并在那里做一些清理,将imageView设置为零,并将其从子视图中删除。
当您的单元出队并即将在tableview中重用时,此方法被调用。

+0

是的,这是答案。非常感谢你! – anyName

相关问题