我動態地添加一個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