2016-12-02 22 views
1

我做了一個UIView和兩個UILabels編程。標籤上寫着這樣的話:「沒有帖子,請關注某人添加到您的Feed。」加入後編程方式刪除的UILabel - 迅速

self.update.count顯示的職位數顯示。所以如果是0就應該顯示我製作的標籤。如果不是,它不應該顯示任何標籤。

我有此代碼,但它不會再次去除標籤和UIView的?它在裏面viewWillAppear

if self.updates.count == 0 { 

      print("THERE ARE NO POSTS: \(self.updates.count)") 
      self.tableView.addSubview(self.noPostView) 
      self.tableView.addSubview(self.noPostLabel) 
      self.tableView.addSubview(self.noPostText) 

      //noPostView.anchorToTop(view.topAnchor, left: nil, bottom: view.bottomAnchor, right: nil) 
      self.noPostView.centerXAnchor.constraintEqualToAnchor(self.tableView.centerXAnchor).active = true 
      //noPostView.centerYAnchor.constraintEqualToAnchor(tableView.centerYAnchor).active = true 
      self.noPostView.anchor(self.view.topAnchor, left: nil, bottom: nil, right: nil, topConstant: 80, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: self.view.frame.width, heightConstant: self.noPostLabel.frame.height + self.noPostText.frame.height) 

      self.noPostLabel.anchor(self.view.topAnchor, left: nil, bottom: nil, right: nil, topConstant: 80, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: self.noPostView.frame.width, heightConstant: 50) 
      self.noPostLabel.centerXAnchor.constraintEqualToAnchor(self.noPostView.centerXAnchor).active = true 


      self.noPostText.anchor(self.noPostLabel.bottomAnchor, left: self.noPostView.leftAnchor, bottom: nil, right: self.noPostView.rightAnchor, topConstant: -20, leftConstant: 35, bottomConstant: 0, rightConstant: 35) 
      self.noPostText.heightAnchor.constraintEqualToConstant(60).active = true 
      self.noPostText.centerXAnchor.constraintEqualToAnchor(self.noPostView.centerXAnchor).active = true 

      self.loadingSpinner.stopAnimating() 
     } else { 
      print("THERE ARE: \(self.updates.count) POSTS") 

      self.tableView.willRemoveSubview(self.noPostView) 
      self.tableView.willRemoveSubview(self.noPostText) 
      self.tableView.willRemoveSubview(self.noPostLabel) 

     } 
+0

只是隱藏的標籤,當你不需要他們即「self.noPostLabel.isHidden = true」,並顯示他們需要的時候。 – DrPatience

+0

工作就像一個魅力@DrPatience:d愚蠢的我........ –

+0

試試這個self.noPostLabel.removeFromSuperview(),我希望這可以幫助你 –

回答

0

您正在使用willRemoveSubview(_:)蘋果在下面描述。

此方法的默認實現不執行任何操作。當子視圖 被移除時,子類可以 覆蓋它以執行其他操作。當子視圖的超級視圖更改時調用此方法,或者在子視圖完全從視圖層次結構中刪除時調用此方法。

0

當你不需要它們時就隱藏標籤,即「self.noPostLabel.isHidden = true」並在需要時顯示它們。