如何在tableView加载时显示活动指示器? [英] How to show activity indicator while tableView loads?

查看:136
本文介绍了如何在tableView加载时显示活动指示器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在我的标签之间切换时,它会加载几秒钟,我想知道我的数据正在加载。为此我决定添加一个活动指标。

When I switch between my tabs it loads some seconds and I want to know that my data is loading. For that I decided to add an activity indicator.

我写了一个小函数:

func showActivityIndicator() {
    dispatch_async(dispatch_get_main_queue()) {
        self.spinner = UIActivityIndicatorView(activityIndicatorStyle: .WhiteLarge)
        self.spinner.frame = CGRect(x: 0.0, y: 0.0, width: 80.0, height: 80.0)
        self.spinner.center = CGPoint(x:self.loadingView.bounds.size.width / 2, y:self.loadingView.bounds.size.height / 2)

        self.loadingView.addSubview(self.spinner)
        self.view.addSubview(self.loadingView)
        self.spinner.startAnimating()
    }
}

将显示我的指标。当我从infoController中点击按钮时尝试使用它:

that will show my indicator. And try to use it when I tapped from my infoController to button:

@IBAction func goToMainFromInfo(sender: AnyObject) {
        self.showActivityIndicator()
        self.performSegueWithIdentifier("fromMainToInfoWActivity", sender: nil)
        self.hideActivityIndicator()
    }
}

我在segue执行之前显示它并在之后隐藏。它对我没有帮助。当我尝试使用同步时:

I show it before segue perform and hide after. It doesn't help me. When I did try to use sync:

@IBAction func goToMainFromInfo(sender: AnyObject) {
    dispatch_async(dispatch_get_main_queue()) {
        self.showActivityIndicator()
        self.performSegueWithIdentifier("fromMainToInfoWActivity", sender: nil)
        self.hideActivityIndicator()
    }
}

但它也没有帮助。当我按Tab键时,不透明度变为0.5,我在加载时等待。但我没有看到我的活动指标。

But it doesn't help too. When I press to tab it opacity becomes 0.5 and I wait while it loading. But I do not see my activity indicator.

有什么问题?

推荐答案

试试这个:

var indicator = UIActivityIndicatorView()

func activityIndicator() {
    indicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 40, 40))
    indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
    indicator.center = self.view.center
    self.view.addSubview(indicator)    
}

您想要开始动画的地方

indicator.startAnimating()
indicator.backgroundColor = UIColor.whiteColor()

止损:

indicator.stopAnimating()
indicator.hidesWhenStopped = true

注意:避免同时调用start和stop 。只是给出一些条件。

Note: Avoid the calling of start and stop in same time. Just give some conditions.

这篇关于如何在tableView加载时显示活动指示器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆