2013-02-22 34 views
0

我有一個要求,我的UITableview應該從Web服務檢索數據後加載數據。當檢索大量數據時,我的uitableview顯示爲空。相反,UIActivityIndi​​cator應該出現,直到數據加載到UITableview中。uitableview應該加載後,從大量的數據從webservice中刪除數據

我想如下,但無法獲得所需的功能。請幫忙。

另外,讓我建議什麼是最好的方法來加載數據到UITableview,當從webservice返回的數據是巨大的。

在此先感謝。

- (UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 

    UILabel *lblName = (UILabel *)[cell viewWithTag:1]; 
    [lblName setText:[myArray objectAtIndex:[indexPath row]]]; 
    return cell; 
} 

- (void)dataLoading 
{ 
    myActivityIndicator=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 
    [myActivityIndicator setFrame:CGRectMake(140, 170, 40, 40)]; 
    [self.view addSubview:myActivityIndicator]; 
    [myActivityIndicator startAnimating]; 

    NSString *playersDatalink = [NSString stringWithFormat:@"Webservice link"]; 

    NSURL *JsonPlayersDataURL = [NSURL URLWithString:playersDatalink]; 
    NSString *JsonPlayersData = [[NSString alloc] initWithContentsOfURL:JsonPlayersDataURL]; 

    SBJSON *playersDataParser = [[SBJSON alloc] init]; 
    NSDictionary *PlayersDicdata = (NSDictionary *) [playersDataParser objectWithString:JsonPlayersData error:nil]; 
    NSDictionary *playersdata = (NSDictionary *) [PlayersDicdata objectForKey:@"players"]; 
    NSLog(@"palyersdata is =%@",playersdata); 
    self.myArray = [NSMutableArray arrayWithCapacity:[playersdata count]]; 
    for (NSDictionary *details in playersdata) { 
     [self.myArray addObject:[details objectForKey:@"teamid"]]; 
    } 
    if ([playersdata count]==0) { 
     UIAlertView *myalert = [[UIAlertView alloc]initWithTitle:@"Webserive error " message:@"No data returned from webservice" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; 
     [myalert show]; 
    } 
} 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self performSelector:@selector(dataLoading) withObject:nil afterDelay:0.0]; 
    [myActivityIndicator stopAnimating]; 

} 
+0

我的朋友重新加載數據在完成加載委託方法可能是你使用NSURLconnection權限的數據庫嗎? – 2013-02-22 05:11:13

回答

1

喜下載這個zip http://www.sendspace.com/file/l48jxg Unzipe這個文件,你獲得兩個.H.M加載Indicatore觀點,。將其拖放到您的項目中。

現在你可以使用這個像貝婁: - 如果你想展示活動指示燈,直到你的數據不會加載

: -

import LoadingView.h into your Class

創建對象

loadingView =[LoadingView loadingViewInView:self.view:@"Please Wait.."]; 在這個時候把這個代碼像olso yourTableView.userInterfaceEnable=FALSE;

,並在數據加載完成的方法把這個

[loadingView removeFromSuperview];

yourTableView.userInterfaceEnable=TRUE;

你得到加載視圖,如: -

enter image description here

UPDATE

它不會改變您的TableView委託首先執行。

1)如果您使用NSURLConnection對Parsiong Web服務,你可以在加載完成後重新裝載YOUT的TableView數據: -

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 


    //your code 
    [yourTableView reloadData]; 

} 

2)如果使用NSXMLParser對Parsiong Web服務,你可以刷新YOUT的TableView加載後的數據完成在: -

- (void)parserDidEndDocument:(NSXMLParser *)parser 
{ 

    //your code 
    [yourTableView reloadData]; 
} 
+0

謝謝@Nitin Gohel,你能不能請建議什麼是從webservice將大量數據加載到UITableview中的最佳方式。在viewDidLoad()中,我將數據加載到數組中。但是,tableview委託方法首先執行。 – 2013-02-22 05:38:18

+0

查看我的更新回答我的朋友:) – 2013-02-22 05:55:49

+0

謝謝@Nitin Gohel。 – 2013-02-22 06:29:12