我的故事板中有一個單元的原型。
基本上所述細胞分成兩個部分:具有自動佈局的UITableViewCell子類如何在運行時更改約束優先級
- 文本視圖
- 的圖像視圖
的細胞與它的值通過一個目的是細胞本身初始化。在該方法中,單元格將其元素設置爲傳遞的值。
主要想法是根據圖像或文本的存在來更改單元佈局。爲此,我已經限制了UITextView
和UIImageView
,一方面增加了兩個限制,不同的優先級。優先級將根據傳遞的對象而改變。
例如UITextView
有兩條虛線,它們表示具有不同優先級和不同常數值的兩個約束。第一個(從LtoR讀取)的優先級爲910,常數爲156,第二個優先級爲900,常數爲20.
在故事板編輯器中,如果我交換了2個優先級,想要實現。
單元代碼:
- (void) updateConstraints {
if (self.postObject.timelinePostText && self.postObject.timelinePostMultimedia) {
self.imageMinusTextViewConstraint.priority = 900;
self.imagePlusTextViewConstraint.priority = 910;
self.textViewMinusImageViewConstraint.priority = 900;
self.textViewPlusImageViewConstraint.priority = 910;
}
else if (self.postObject.timelinePostMultimedia) {
self.imageMinusTextViewConstraint.priority = 910;
self.imagePlusTextViewConstraint.priority = 900;
}
else if (self.postObject.timelinePostText) {
self.textViewMinusImageViewConstraint.priority = 910;
self.textViewPlusImageViewConstraint.priority = 900;
}
[super updateConstraints];
}
- (void) configureCellWith: (AFTimelinePostObject*) postObject {
self.postObject = postObject;
self.userNameLabel.text = postObject.timelinePostOriginalPoster;
self.dateLabel.text = [[AFTimelinePostObject dateFormatterInternet] stringFromDate:postObject.timelinePostDateObject];
self.postTitleLabel.text = postObject.timelinePostTitle;
self.multimediaMediaType = postObject.timelinePostMultimedia ? [self checkMediaTypeAtPath: postObject.timelinePostMultimedia] : kMediaTypeUnknown;
if (postObject.timelinePostMultimedia && postObject.timelinePostText) {
[self.postImageView setImageWithURL:[NSURL URLWithString:postObject.timelinePostMultimedia] placeholderImage:nil];
self.postTextView.text = postObject.timelinePostText;
}
else if (postObject.timelinePostMultimedia) {
[self bringSubviewToFront:self.postImageView];
[self.postImageView setImageWithURL:[NSURL URLWithString:postObject.timelinePostMultimedia] placeholderImage:nil];
}
else if (postObject.timelinePostText) {
[self bringSubviewToFront:self.postTextView];
self.postTextView.text = postObject.timelinePostText;
self.postImageView.image = nil;
}
[self setNeedsUpdateConstraints];
[self updateConstraintsIfNeeded];
[self setNeedsLayout];
[self layoutIfNeeded];
}
刪除的看法是不是一個解決方案,因爲細胞被回收,如果需要,我要重新添加,但這樣我想我錯過了性能優勢與回收有關。
不幸的是,細胞仍然被平分爲文本和圖像,而不會根據傳遞的數據改變它們的幀。
我錯過了什麼?
感謝利奧,爲您提供建議。其實生產代碼使用不同的單元格的可能性。我不想改變這個常數,因爲我認爲我會錯過只在故事板編輯器上工作的可能性,是的,我可以在兩者之間交換常量值。這裏的要點是,我想要做的事情是可能的,我很確定,但我沒有明白爲什麼不工作,或者不知道我想確保這是不可能的。 – Andrea
@Andrea在我看來,你仍然可以看到文本視圖和圖像視圖的原因是因爲約束之間沒有「戰鬥」,所以它沒有優先權,因爲它就是這樣,單元滿足佈局約束。 –
thx支持我!我找到了解決方案。 – Andrea