2012-05-07 53 views

回答

0

只是調用Web服務,當它結束加載時,將響應的對象放入一個數組中,並將其傳回到服務被調用的類中,用數據源數組替換objects數組,並調用[tablview reloadData ]。

+0

數據源數組意味着你將顯示數據到tableview單元格的數組 – Saad

+0

請你可以更清楚地告訴我情況....我沒有得到它 – Pradeep

0

這個工作可以做到以下幾點:

起初創建視圖 - 控制裏面的屬性,將舉行您將收到來自您的Web服務中的數據,這樣的:

@property (strong,nonatomic) NSMutabledata *theData; 

然後合成它在實現文件中,使用

@synthesize theData = _theData; 

下一步你需要建立一個NSURLConnection的,這實際上會從你的web服務加載數據:

NSURL *theURL = [NSURL urlWithString:@"http://thisisyourwebservice.com/somethinghere"]; 
NSURLRequest *theRequest = [NSURLRequest requestWithURL:theURL]; 
NSURLConnection *connection = [NSURLConnection connectionWithRequest:theRequest delegate:self]; 

您可以在viewDidLoad方法內或在自定義設置方法中設置此項。如果你希望這個連接可以被取消,就像有人解散你的視圖一樣,你需要像爲數據一樣添加一個屬性。

到目前爲止,這將創建一個連接,它會自動開始從給定的URL下載數據。但目前你的數據將無處可去,因爲你還沒有實現NSURLConnectionDataDelegate協議。你這樣做如下:

裏面你的頭文件做類似如下:

@implementation YourViewControllerClass : UIViewController <NSURLConnectionDataDelegate> 

現在您需要實現您的視圖控制器內的一些委託方法,所以你其實可以接收數據並保存它供以後使用。那些將是:

/* you received a response, so now initialize the NSMutableData object */ 
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    /* this should work in ARC, without ARC you would have to add autorelease after init, else you will have a leak */ 
    self.theData = [NSMutableData alloc] init]; 
} 

/* here you will actually receive data */ 
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    [self.theData appendData:data]; 
} 

/* now your connection has finished loading */ 
- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    /* depending on what you are loading, you need to convert your data into an object 
    you of your choice. if you loaded JSON, then use one of the JSONparsers like 
    SBJSON, JSONKit or the NSJSONSerialization class available since iOS 5.0. 

    After converting your data to the expected object type you will assign it to the 
    property which you are using as datasource for your tableView. 
    */ 
} 

現在,在連接加載後,您實際上應該有一個屬性與您的viewcontroller內的所需數據。現在只需使用[yourTableView reloadeData]重新加載你的tableView,你應該很好去!

相關問題