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,所以一旦視圖控制器打開,我想要活動指示器踢,直到整個表視圖填充和單元格正確調整大小。 –