2014-04-17 82 views
1

我的故事板中有一個單元的原型。
基本上所述細胞分成兩個部分:具有自動佈局的UITableViewCell子類如何在運行時更改約束優先級

  • 文本視圖
  • 的圖像視圖

的細胞與它的值通過一個目的是細胞本身初始化。在該方法中,單元格將其元素設置爲傳遞的值。
主要想法是根據圖像或文本的存在來更改單元佈局。爲此,我已經限制了UITextViewUIImageView,一方面增加了兩個限制,不同的優先級。優先級將根據傳遞的對象而改變。
enter image description here 例如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]; 

} 


刪除的看法是不是一個解決方案,因爲細胞被回收,如果需要,我要重新添加,但這樣我想我錯過了性能優勢與回收有關。
不幸的是,細胞仍然被平分爲文本和圖像,而不會根據傳遞的數據改變它們的幀。
我錯過了什麼?

回答

1

不要刪除約束的視圖。相反,將您在xib/storyboard中設置的限制條件連接到代碼中的插座,並修改configureCell方法中的constant值。不要玩重點。根據是否有圖像或文字更改常量。

如果您的邏輯只允許要麼文本單元圖像單元,您可以設置兩個不同的細胞具有不同的標識符,並相應地出列。這會簡化你的配置邏輯。

另外,代替

[self setNeedsUpdateConstraints]; 
[self updateConstraintsIfNeeded]; 
[self setNeedsLayout]; 
[self layoutIfNeeded]; 

直接讓[self setNeedsLayout];

+0

感謝利奧,爲您提供建議。其實生產代碼使用不同的單元格的可能性。我不想改變這個常數,因爲我認爲我會錯過只在故事板編輯器上工作的可能性,是的,我可以在兩者之間交換常量值。這裏的要點是,我想要做的事情是可能的,我很確定,但我沒有明白爲什麼不工作,或者不知道我想確保這是不可能的。 – Andrea

+0

@Andrea在我看來,你仍然可以看到文本視圖和圖像視圖的原因是因爲約束之間沒有「戰鬥」,所以它沒有優先權,因爲它就是這樣,單元滿足佈局約束。 –

+0

thx支持我!我找到了解決方案。 – Andrea

1

嗯,我抽出一個小樣本,使事情變得簡單。 這裏有兩種基本的錯誤:

  1. 重點不應該在updateConstraints更新,我仍然試圖找到這種方法的江豚一個相當明確的答案,但似乎如果你加你應該只使用(可能刪除不考慮)約束
  2. 我送 - bringSubViewToFront到錯誤的家長,正確的是-contentView

下面是正確的代碼,感謝@Leo對我的支持。

//- (void) updateConstraints { 
// if (_postObject[TEXT_KEY] && _postObject[IMAGE_NAME_KEY]) { 
//  self.imageMinusTextViewConstraint.priority = 800; 
//  self.imagePlusTextViewConstraint.priority = 999; 
//  self.textViewMinusImageViewConstraint.priority = 800; 
//  self.textViewPlusImageViewConstraint.priority = 999; 
//   
// } 
// else if (_postObject[IMAGE_NAME_KEY]) { 
//  self.imageMinusTextViewConstraint.priority = 999; 
//  self.imagePlusTextViewConstraint.priority = 800; 
// } 
// else if (_postObject[TEXT_KEY]) { 
//  self.textViewMinusImageViewConstraint.priority = 999; 
//  self.textViewPlusImageViewConstraint.priority = 800; 
// } 
// 
// [super updateConstraints]; 
//} 
// 
// 
//- (void) layoutSubviews { 
// [super layoutSubviews]; 
//} 



- (void) configureCellWith: (NSDictionary*) postObject { 
    //Configurare gli elmenti già presenti 
    self.postObject = postObject; 

    //Check the presence of some kind of data 
    if (postObject[TEXT_KEY] && postObject[IMAGE_NAME_KEY]) { 
     self.imageMinusTextViewConstraint.priority = 800; 
     self.imagePlusTextViewConstraint.priority = 999; 
     self.textViewMinusImageViewConstraint.priority = 800; 
     self.textViewPlusImageViewConstraint.priority = 999; 
     self.postImageView.image = [UIImage imageNamed:postObject[IMAGE_NAME_KEY]]; 
     self.postTextView.text = postObject[TEXT_KEY]; 

    } 
    else if (postObject[IMAGE_NAME_KEY]) { 
     self.imageMinusTextViewConstraint.priority = 999; 
     self.imagePlusTextViewConstraint.priority = 800; 
     self.postImageView.image = [UIImage imageNamed:postObject[IMAGE_NAME_KEY]]; 
     self.postTextView.text = nil; 
     [self.contentView bringSubviewToFront:self.postImageView]; 

    } 
    else if (postObject[TEXT_KEY]) { 
     self.textViewMinusImageViewConstraint.priority = 999; 
     self.textViewPlusImageViewConstraint.priority = 800; 

     self.postTextView.text = postObject[TEXT_KEY]; 
     self.postImageView.image = nil; 
     [self.contentView bringSubviewToFront:self.postTextView]; 

    } 

// [self setNeedsLayout]; 
// [self layoutIfNeeded]; 



} 
相關問題