簡體   English   中英

UITableView重新加載未使用DispatchQueue.main.async更新表視圖

[英]UITableView Reload Not Updating the tableview with DispatchQueue.main.async

我已經用swift4編寫了代碼,並在iPhone模擬器上運行。

DispatchQueue.main.async {
     self.view.activityindicatorStop()
     self.mytableView.reloadData()
}

上面的代碼有以下操作

數組填充數據后,停止活動指示器並更新TableView

但是預期不會發生。

我得到了關注

  1. 活動指示器已停止,但指示器未移除且背景視圖未移除。
  2. TableView未更新。

我使用的代碼:

 super.viewDidLoad()


        UserDefaults.lastAccessDate = Date()


        self.view.activityStartAnimating(activityColor: #colorLiteral(red: 0.9176470588, green: 0.2901960784, blue: 0.262745098, alpha: 1), backgroundColor: #colorLiteral(red: 1, green: 1, blue: 1, alpha: 0))

       //Calling the API  
      Capsulle.retriveSpaceTimeCapsulle { (data, respose, error) in
        if nil == error
        {
         let ServerData = serverComm.toJSONObject(with: data!)
         let result = ServerData.0 as? Array<Dictionary<String, Any>>
         self.capsulleDetail = result

        if (result == nil)
        {
            if false ==  loginCredential.isHavingAccessToken
            {
            loginCredential.isHavingAccessToken = false;
            UserDefaults.standard.set( "", forKey: "UserEmailID")
            let objStoryBoard:UIStoryboard = UIStoryboard.init(name: "Main", bundle: nil)
            let initialViewController:UIViewController = objStoryBoard.instantiateViewController(withIdentifier: "loginVC")

            self.present(initialViewController, animated: true, completion: {

            })

            return
            }
        }


         DispatchQueue.main.sync {

            self.CapsulleCollectionView.reloadData()

            self.view.activityStopAnimating()

         }
         }

      }

    }

func activityStartAnimating(activityColor: UIColor, backgroundColor: UIColor) {
        let backgroundView = UIView()
        backgroundView.frame = CGRect.init(x: 0, y: 0, width: self.bounds.width, height: self.bounds.height)
        backgroundView.backgroundColor = backgroundColor
        backgroundView.tag = 475647

        var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
        activityIndicator = UIActivityIndicatorView(frame: CGRect.init(x: 0, y: 0, width: 50, height: 50))
        activityIndicator.center = self.center
        activityIndicator.hidesWhenStopped = true
        activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
        activityIndicator.color = activityColor
        activityIndicator.startAnimating()
        self.isUserInteractionEnabled = false

        backgroundView.addSubview(activityIndicator)

        self.addSubview(backgroundView)
    }

func activityStopAnimating() {
    if let background = self.viewWithTag(475647){

        background.removeFromSuperview()
    }
    self.isUserInteractionEnabled = true
}

我已經創建了activityStopAnimating和activityStartAnimating作為擴展

DispatchQueue.main.async {
    self.view.activityIndicator.stopAnimating()
    self.view.activityIndicator.isHidden = true
    self.mytableView.beginUpdates()
    self.mytableView.endUpdates()
}

您的代碼應該適用於tableView重新加載,但是您也可以嘗試使用beginUpdates()endUpdates()方法。 並要stopActivityindicator調用stopAnimating() ,然后隱藏您的activityindicator,此代碼應為您工作

享受編碼。

要重新加載tableView,必須確保self.capsulleDetail數據已更改(如果此數組是您的數據源),我認為它沒有更改或仍為零,因此reloadData()不會執行任何操作

要隱藏指示器,您應該將此屬性設置為在停止時隱藏它,或者通過將is-hidden-hidden隱藏為true來直接隱藏它

self.activityIndicator.hidesWhenStopped = true

我找不到任何正確的解決方案來完美地做到這一點。

長期運行后,我決定進行以下操作,它正在按我的預期工作。

DispatchQueue.main.async {
                        // now update UI on main thread

                        self.myTableView.reloadData()
                        self.myTableView.setContentOffset(CGPoint(x: 0, y: -1), animated: true)
                        self.view.activityStopAnimating()

                    }

感謝您的所有答復。.我正在等待完美的答復。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM