添加后,以编程方式删除uilabel -swift



我已经编程了一个uiview和两个uilabels。标签说:"没有帖子,请跟随某人添加到您的提要中。"。

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)
        }

当您不需要时即可隐藏标签,即self.nopostlabel.ishidden = true",并在需要时显示它们。

您正在使用willremovesubview(_ :)苹果在下面描述的。

此方法的默认实现无济于事。子类可以 覆盖它在子视图时执行其他操作 删除。当子视图的监督更改或 当该子视图完全从视图层次结构中删除时。

最新更新