2013-09-24 136 views
-1

嗨,我有一個UITableView。它從Web服務加載多個數據。我想加載這個tableview 10 10.最初它加載前10個項目。當用戶滾動到UITableView的末尾時,它應該從服務器加載下一個10條記錄。所以在我scrollviewDidEndDeclarating委託我把這樣的UITableView部分加載

`

​​

但問題是,當我阻止它被卡住,直到裝載表視圖中的滾動。任何人都可以給我一個解決方案,這

感謝

+1

這聽起來像你不加載數據不同步。 –

+0

,因爲你的web服務不是異步的..所以它只是等到下10條記錄來 –

+0

我該怎麼做? – iDia

回答

0

嘗試NSURLConnection的,這將有助於您致電異步Web服務

一個NSURLConnection的對象用於執行使用HTTP web服務的執行。 使用NSURLConnection時,請求以異步形式進行。這是否意味着你不等待,繼續請求結束,

此委託必須實現以下方法:

connection:didReceiveResponse : called after the connection is made successfully and before receiving any data. Can be called more than one time in case of redirection. 
connection:didReceiveData : called for each bloc of data. 
connectionDidFinishLoading : called only one time upon the completion of the request, if no error. 
connection:didFailWithError : called on error. 

實例: -

NSData *data = [[NSMutableData alloc] init]; 

    NSURL *url_string = [NSURL URLWithString: 
        @"Your URL"]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url_string]; 
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request 
                    delegate:self]; 
    if (!conn) { 
     // this is better if you @throw an exception here 
     NSLog(@"error while starting the connection"); 
     [data release]; 
    } 

爲每塊接收到的原始數據可以用這種方法在這裏附加數據:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)someData { 
    [data appendData:someData]; 
} 

connectionDidFinishLoading將在成功數據的末尾調用receivied

0

使用這種代碼加載更多的行動

- (void)scrollViewDidScroll:(UIScrollView *)scrollView 
{ 
    //scrollView.contentSize.height-scrollView.frame.size.height indicates UItableView scrool end 
    if (scrollView.contentOffset.y >= scrollView.contentSize.height-scrollView.frame.size.height) 
    { 
     if(loadMore) 
     { 
      loadmore=no; 
      //call your Web service 
     } 
    } 
}