2012-09-25 43 views
1

在我的應用程序中,我從數據庫中獲取數據。我從數據庫獲得的數據非常大,如20000條記錄。所以,我想顯示一個活動指示器來指示數據正在加載。但是當我運行該應用程序時,不顯示活動指示器。從sqlite數據庫加載大量數據時未顯示活動指標

我要發送此代碼。請幫助我。

activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 

activityIndicator.frame = CGRectMake(100,100, 20, 20); 
[self.view addSubview:activityIndicator]; 

[activityIndicator startAnimating]; 

// Code to get data from database 

[activityIndicator stopAnimating]; 

回答

3

首先,您不需要爲activiyIndi​​cator設置框架。你可以設置中心...

activityIndicator.center = self.view.center; (or soemthing like this). 

第二,你如何檢索數據?你是在主線上做這一切嗎?或者你是否在異步執行它?

::編輯::

啊看來你這樣做......

[actInd startAnimating]; 
[self getData]; //this is asynchronous 
[actInd stopAnimating]; 

,如果是這樣的話,則stopAnimating將得到幾乎立即調用。

你需要的是這樣的事情...

[actInd startAnimating]; 
[self getData]; //this is asynchronous 
中的getData

則...

- (void)getData 
{ 
    //get all your data and process it.... 

    [actInd stopAnimating]; 
} 

通過這種方式,停止動畫只會被做所有的數據下班後調用。

如果這樣做不起作用,嘗試刪除stopAnimating完全檢查它實際上是首先動畫。

+0

我做它異步'[activityIndi​​cator startAnimating]; array = [SQLite getDataFromDB]; [activityIndi​​cator stopAnimating];'編輯 – smily

+0

以迴應評論。 – Fogmeister

+0

謝謝Fogmeister,你的建議對我有用 – smily

0

這樣做:

activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 
activityIndicator.center = self.view.center; 
[self.view addSubview:activityIndicator]; 
[NSThread detachNewThreadSelector:@selector(startActivityIndicator) toTarget:self withObject:nil]; 

添加這個方法:

-(void)startActivityIndicator 
{ 
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 
    [activityIndicator startAnimating]; 
    [pool release]; 
} 

做變化:

- (void)getData 
{ 
    //get all your data from database. 
    [activityIndicator stopAnimating]; 
} 
0

@Fogmeister是有關設置activityIndi​​cator的框架賴特。

和顯示activityIndi​​cator只需創建在你的問題中的代碼的一個功能,並使用performSelector:withObject:afterDelay:,0.001延遲稱它爲和activityIndi​​cator將顯示

編碼快樂:)

相關問題