我想先調用webservice然後生成表視圖,我如何才能實現這個請幫助我。我想先調用webservice,然後在iphone中生成表格視圖
0
A
回答
0
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,你應該很好去!
相關問題
- 1. 我想先調用警報視圖,然後再進一步NSManagedObject
- 2. 先加載視圖,然後填充我的表格視圖
- 3. 調用函數,然後在視圖中選擇一個表格
- 4. 我想把表在asp.net網格視圖
- 5. 在iPhone中刪除子視圖後刷新表格視圖
- 6. iPhone滾動視圖/動態文本標籤,然後表格視圖和圖像
- 7. 不想網格視圖模板字段生成excel表
- 8. 切換視圖後仍然在視圖中調用方法?
- 9. 調整表格單元格中的圖像視圖iphone
- 10. 如何檢測哪個單元格被點擊然後根據它在iphone uitableview中生成下一個視圖?
- 11. 我想在Ajax調用之後呈現部分視圖
- 12. iPhone - 在界面生成器中自定義分組表格視圖
- 13. 我想調用要在視圖中顯示的圖像
- 14. 生成自定義表格視圖,表格視圖單元格不能放入表格視圖行
- 15. 預先生成視圖後需要做什麼
- 16. 如何在後臺iphone中定期調用webservice?
- 17. 表格視圖 - 顯示像 - #我周圍 - 谷歌# - 在iPhone中
- 18. 表格/圖形報表生成器3.0圖表不想保持在位置
- 19. iPhone界面生成器做一個視圖中的子視圖
- 20. 在循環/ foreach MVC視圖中動態生成表格
- 21. 當我向下滾動表格視圖並向上滾動表格視圖時,我不想在表格視圖中重新加載單元格
- 22. 在iPhone中滾動表格視圖時,表格視圖的狀態發生變化
- 23. 我想在我的iPhone App,以顯示我的身後表視圖的自定義圖像
- 24. 我有一個表格視圖,我想傳播表格單元格與間隙
- 25. iphone從圖像生成視頻
- 26. 改變表格視圖使用#iphone
- 27. iPhone表格視圖,使用NSDictionary&plist
- 28. Iphone中的分組表格視圖
- 29. 在我的視圖中對生成的表格進行排序(visual studio.net)
- 30. 我想修復我的表格視圖,而我滾動
你到目前爲止做了什麼?請分享一些代碼。你用數組填充你的數據還是什麼? – HelmiB