Что корректный путь состоит в том, чтобы удалить подпредставление из иерархии представления и уничтожить его?

Я думаю, вы забыли установить правильное изображение на тот случай, если вы ищете. Так как ячейки табличного представления повторно используются iOS, она сохраняет изображение, которое было установлено ранее, поэтому вы должны установить его снова. Попробуйте это:

if searching{
        cell?.UnitName?.text = searchUnits[indexPath.row]
        cell?.UnitBild.image = UIImage(named : searchUnits[indexPath.row])
    } else {
        cell?.UnitName.text = units[indexPath.row]
        cell?.UnitBild.image = UIImage(named : units[indexPath.row])
    }
38
задан Matt Fenwick 22 December 2014 в 19:29
поделиться

5 ответов

Try this:

UIView *v = [self.containerView viewWithTag:[n integerValue]];
v.hidden = YES;
[self.containerView bringSubviewToFront:v];
[v removeFromSuperview];

Another thing I just noticed from the UIView class document - see the last sentence:

removeFromSuperview Unlinks the receiver from its superview and its window, and removes it from the responder chain.

  • (void)removeFromSuperview

Discussion If the receiver’s superview is not nil, this method releases the receiver. If you plan to reuse the view, be sure to retain it before calling this method and be sure to release it as appropriate when you are done with it or after adding it to another view hierarchy.

Never invoke this method while displaying.

UPDATE: It is now 2014 and removing a subview without hiding it works perfectly fine. The original poster's code should work as-is:

UIView *v = [self.containerView viewWithTag:[n integerValue]];
[v removeFromSuperview];

This will remove v and any views it has attached to it as subviews, leaving behind containerView and any siblings of v.

68
ответ дан 27 November 2019 в 03:10
поделиться
    override func viewWillAppear(_ animated: Bool)
    {
        super.viewWillAppear(animated)

        if let topController = UIApplication.topViewController() {

            if topController.isKind(of: ProviderHome.self)
            {
                let arrOfSuview = self.view.subviews

                if arrOfSuview.count > 1
                {
                    print("Davender Arr of subviews : \(arrOfSuview)")

                    for i in 0..<arrOfSuview.count
                    {
                        let objSub = arrOfSuview[i]

                        if objSub.tag == 101
                        {
                          objSub.removeFromSuperview()
                        }

                    }



                }


                NotificationCenter.default.addObserver(self, selector: #selector(ProviderHome.handelPushNotification), name: NSNotification.Name(rawValue: "handelPush"), object: nil)

                NotificationCenter.default.addObserver(self, selector: #selector(ProviderHome.handelLocalNotification), name: NSNotification.Name(rawValue: "handelLocal"), object: nil)
            }
        }


    }

@objc func handelPushNotification(_ notification: NSNotification)  {


        let arrOfSuview = self.view.subviews

        if arrOfSuview.count > 1
        {
            print("Davender Arr of subviews : \(arrOfSuview)")

            for i in 0..<arrOfSuview.count
            {
                let objSub = arrOfSuview[i]

                if objSub.tag == 101
                {
                    objSub.removeFromSuperview()
                }

            }

        }

        if notification.userInfo != nil
        {


            let dict = notification.userInfo as! Dictionary<String, Any>

            let d = dict["data"] as! Dictionary<String, Any>

            let action = d["gcm.notification.label"] as! String

            print("current message id :- ", action)

            self.getNotificationId = action

            if getNotificationId != ""
            {
               //call the api for getting Data

                AppDelegate.sharedInstance().myCurrentnotificationId = getNotificationId

                //working code
                let storyboard = UIStoryboard(name: "Provider", bundle: nil)
                let vc = storyboard.instantiateViewController(withIdentifier: "CommonPopUpsVC") as! CommonPopUpsVC
                vc.modalPresentationStyle = .overFullScreen
                vc.view.frame = self.view.frame
                vc.view.tag = 101
                self.view.addSubview(vc.view)
                self.present(vc, animated: true, completion: nil)

            }


       }
    }
0
ответ дан Davender Verma Soni 27 November 2019 в 03:10
поделиться

Это правильная общая идея. те другие UIView, которые исчезают, как они связаны с этим UIView? Являются ли они частями этой точки зрения? Освобождены ли они в методе dealloc для удаляемого представления?

Вы уверены, что ваши теги уникальны?

Sujal

0
ответ дан 27 November 2019 в 03:10
поделиться

Is it possible that cell.contentView has the same tag as the subview you want to remove? according to the documentation viewWithTag removes:

The view in the receiver’s hierarchy that matches tag. The receiver is included in the search.

If this is the case then you may be inadvertently removing cell.contentView from the cell. If n is zero and your cell's contentview has no tag set to it, it would default to 0 and cause that to happen.

0
ответ дан 27 November 2019 в 03:10
поделиться

Они просто исчезают с дисплея или исчезают с дисплея и иерархии просмотра? Что вам показывает отладчик?

0
ответ дан 27 November 2019 в 03:10
поделиться
Другие вопросы по тегам:

Похожие вопросы: