2016-10-07 93 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

相關問題