无法在 API 调用结束时隐藏活动指示器

Posted

技术标签:

【中文标题】无法在 API 调用结束时隐藏活动指示器【英文标题】:Cannot Hide Activity indicator at end of API call 【发布时间】:2015-08-28 11:07:47 【问题描述】:

我有一个表格视图,其中数据是从 API 调用加载的。我显示了一个加载叠加视图。我使用以下代码来设置加载整体视图。

https://coderwall.com/p/su1t1a/ios-customized-activity-indicator-with-swift

但问题是,一旦加载了表格视图,我似乎无法隐藏覆盖视图或活动指示器。它只是继续加载。

要启动活动指示器,使用以下代码:

 ViewControllerUtils().showActivityIndicator(self.view)

阻止它:

 ViewControllerUtils().hideActivityIndicator(self.view)

我的 API 调用代码如下:

 import UIKit

 class BioListTableViewController: UITableViewController 

// variable to hold the index value of the cell tapped
var cellTapped = Int()


@IBOutlet var tableview: UITableView!

var bioArray = NSArray()
    didSet

        dispatch_async(dispatch_get_main_queue())
               self.tableview.reloadData()
            


override func viewDidLoad()

    super.viewDidLoad()
    self.tableView.rowHeight = 80.0



    // A switch Statement to check which cell was tapped and which API link to call
    switch cellTapped
    

       case 0:
         test()
       case 1:
         test()
       default:
         test()

    

   

override func viewDidAppear(animated: Bool)

    ViewControllerUtils().hideActivityIndicator(self.view)


func test() 


     ViewControllerUtils().showActivityIndicator(self.view)
    println("This is TWEST")

    var request = NSMutableURLRequest(URL: NSURL(string: "APILink")!)
    var session = NSURLSession.sharedSession()
    request.HTTPMethod = "GET"
    UIApplication.sharedApplication().networkActivityIndicatorVisible = true


    var err: NSError?

    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    request.addValue("application/json", forHTTPHeaderField: "Accept")

    var task = session.dataTaskWithRequest(request, completionHandler: data, response, error -> Void in
    println("Response: \(response)")
    var strData = NSString(data: data, encoding: NSUTF8StringEncoding)
    println("Body: \(strData)")
    var err: NSError?
    var json = NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves, error: &err) as? NSArray

    UIApplication.sharedApplication().networkActivityIndicatorVisible = true


    // Did the JSONObjectWithData constructor return an error? If so, log the error to the console
    if(err != nil) 

    println(err!.localizedDescription)
    let jsonStr = NSString(data: data, encoding: NSUTF8StringEncoding)
    println("Error could not parse JSON: '\(jsonStr)'")

    dispatch_async(dispatch_get_main_queue()) 

    var alert = UIAlertController(title: "Alert", message: "Seems to be an error with server. Try Again Later", preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
    self.presentViewController(alert, animated: true, completion: nil)

    
    else 

    UIApplication.sharedApplication().networkActivityIndicatorVisible = false
        //To to long tasks
       // LoadingOverlay.shared.hideOverlayView()
    // The JSONObjectWithData constructor didn't return an error. But, we should still
    // check and make sure that json has a value using optional binding.
    //var newWeather = WeatherSummary(id:"")

    if let parseJSON = json 


    self.bioArray = parseJSON

    
    else 
    // Woa, okay the json object was nil, something went worng. Maybe the server isn't running?
    let jsonStr = NSString(data: data, encoding: NSUTF8StringEncoding)
    println("Error could not parse JSON: \(jsonStr)")

    

    )

    task.resume()


func testTwo()

    println("THIS IS TEST 2")


override func didReceiveMemoryWarning() 
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.


// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int 
    // #warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1


override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    return self.bioArray.count ?? 0



override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
    let cell = tableView.dequeueReusableCellWithIdentifier("bioCell", forIndexPath: indexPath) as! UITableViewCell

    // Configure the cell...

    let weatherSummary: AnyObject = bioArray[indexPath.row]

    if let id = weatherSummary["employeeName"] as? String
    
        cell.textLabel?.text = id

    

    if let job = weatherSummary["sector"] as? String 
        cell.detailTextLabel?.text = job

    

    var fram = cell.imageView?.frame
    //fram? = CGRectMake( 0, 0, 50, 55 )
    let imageSize = 50 as CGFloat
    fram?.size.height = imageSize
    fram?.size.width = imageSize
    cell.imageView!.frame = fram!
    cell.imageView!.layer.cornerRadius = imageSize / 2.0
    cell.imageView!.clipsToBounds = true
    //cell.imageView?.image = UIImage(named: "userIcon.png")

    if let userImage = weatherSummary["userImage"] as? String
        if   let url = NSURL(string: userImage)
            if let data = NSData(contentsOfURL: url)
                if let image:UIImage = UIImage(data: data)
                    cell.imageView?.image = image
                

            
        

    


    return cell

【问题讨论】:

这可以帮助你:***.com/questions/28865659/hide-activity-indicator 遇到了同样的问题。当我找不到解决方案时,我选择了另一个。 @iRealMe 你用的是哪一个? 现在没有链接。所以我将其发布为答案 【参考方案1】:

尝试在主队列中关闭并在您从服务器获得响应时关闭它

dispatch_async(dispatch_get_main_queue(), 

            ViewControllerUtils().hideActivityIndicator(self.view)

            ))

【讨论】:

感谢您的回复。我尝试过这个。它仍然一直显示它。难道是实际的隐藏功能有问题? 同样的问题。在我收到回复时尝试将其关闭,但它没有将其关闭。【参考方案2】:

您可以使用我从某个地方获得的另一个自定义活动指示器。最简单有效的一种。 (虽然它有带指示器的文本,但如果您不想显示文本,那么您可以按照自己的方式自定义)

创建一个类

class customActivityIndicator: UIVisualEffectView 

var text: String? 
    didSet 
        label.text = text
    

let activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.White)
let label: UILabel = UILabel()
let blurEffect = UIBlurEffect(style: .Dark)
let vibrancyView: UIVisualEffectView

init(text: String) 
    self.text = text
    self.vibrancyView = UIVisualEffectView(effect: UIVibrancyEffect(forBlurEffect: blurEffect))
    super.init(effect: blurEffect)
    self.setup()


required init(coder aDecoder: NSCoder) 
    self.text = ""
    self.vibrancyView = UIVisualEffectView(effect: UIVibrancyEffect(forBlurEffect: blurEffect))
    super.init(coder: aDecoder)
    self.setup()


func setup() 
    contentView.addSubview(vibrancyView)
    vibrancyView.contentView.addSubview(activityIndicator)
    vibrancyView.contentView.addSubview(label)
    activityIndicator.startAnimating()


override func didMoveToSuperview() 
    super.didMoveToSuperview()

    if let superview = self.superview 

        let width: CGFloat = 150
        let height: CGFloat = 50.0
        self.frame = CGRectMake(superview.frame.size.width / 2 - width / 2,
                                superview.frame.height / 2 - height,
                                width,height)

        vibrancyView.frame = self.bounds

        let activityIndicatorSize: CGFloat = 40
        activityIndicator.frame = CGRectMake(5, height / 2 - activityIndicatorSize / 2,
            activityIndicatorSize,
            activityIndicatorSize)

        layer.cornerRadius = 8.0
        layer.masksToBounds = true

        label.text = text
        label.textAlignment = NSTextAlignment.Center
        label.frame = CGRectMake(activityIndicatorSize + 5, 0, width - activityIndicatorSize - 20, height)
        label.textColor = UIColor.grayColor()
        label.font = UIFont.boldSystemFontOfSize(16)

    


func show() 
    self.hidden = false


func hide() 
    self.hidden = true

 

要使用这个:

let spinner = customActivityIndicator(text: "Loading")
//To start animating
self.view.addSubview(spinner)

 //To hide
 self.spinner.hide()

这可能会对你有所帮助。

【讨论】:

以上是关于无法在 API 调用结束时隐藏活动指示器的主要内容,如果未能解决你的问题,请参考以下文章

Firebase .childAdded 和活动指示器

加载网页时,活动指示器不会隐藏

SFSafariViewController 在推入 UINavigationController 时隐藏网络活动指示器

加载网页时活动指示器不隐藏

隐藏活动指示器

集成到搜索栏中的活动指示器不会显示在iPhone SDK中