0

尽管我在viewDidLoad中编写了下面的代码,但我无法在静态表视图上显示活动指示符。无法在静态tableview上显示活动指示灯

let activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray) 
tableView.backgroundView = activityIndicatorView 
tableView.separatorStyle = UITableViewCellSeparatorStyle.none 
self.activityIndicatorView = activityIndicatorView 
self.activityIndicatorView.isHidden = false 
self.tableView.addSubview(activityIndicatorView) 
self.activityIndicatorView.startAnimating() 
+1

你的意思PullToRefresh在TableView中??? – Vandana

+2

如果你想要拉动刷新,那么只需使用刷新控制 –

+0

如果你想要表头,然后启动它作为一个表视图标题而不是子视图,并使用UIRefreshControl如果你想要拉刷新。 – sourav

回答

1

而不是在tableView加入UIActivityIndicatorView,将其添加到您的主视图中,并调用bringSubview(toFront:)主视图带来indicatorView到前面,你have't设定的原点位置为您indicatorView集也。

self.activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray) 
tableView.separatorStyle = UITableViewCellSeparatorStyle.none 
self.activityIndicatorView.isHidden = false 

//Add inside your view 
self.view.addSubview(activityIndicatorView) 

//Set activityIndicatorView origin in the screen 
self.activityIndicatorView.center = self.view.center 

//Try to bring activityIndicatorView to front 
self.view.bringSubview(toFront: activityIndicatorView) 

self.activityIndicatorView.startAnimating() 
+0

它是self.view或self.tableView? @Nirav D –

+0

@RohitDulam我说的是使用'self.view'而不是'self.tableView'来添加activityIndi​​cator。 –

+0

对不起,我正确地看看它。 @Nirav D –

0

你不能将子视图添加到tableview,请尝试将其添加到导航控制器视图。

,如:

self.view.superviewaddSubview(activityIndicatorView); 
+0

感谢您的建议,但它没有解决我的问题。 @Ninja Hattori –

0

如果使用拉刷新,然后执行此代码:

var refreshControl: UIRefreshControl! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    refreshControl = UIRefreshControl() 

    refreshControl.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged) 
    tableView.addSubview(refreshControl) 
} 

func refresh(sender:AnyObject) { 
    tableView.reloadData() 
    refreshControl.endRefreshing() 
} 
+0

这是Objective c和OP正在用Swift –

+0

在swift中检查可编辑的代码,我认为这会对你有所帮助。 – Vandana

+0

我不想拉刷新。基本上我试图用我从服务器获得的数据填充tableview,所以一旦视图控制器打开,我想要活动指示器踢,直到整个表视图填充和单元格正确调整大小。 –