2010-06-30 91 views
1

我無法讓我的UITableViewController重新加載/刷新數據。UITableViewController無法重新加載數據 - ASIHTTPRequest

我有一個方法,它使用ASIHTTPRequest來調用一個URL並帶回一些字符串數據。我試圖把這些數據放到表格的單元格中,但[self.tableview reloadData]不起作用。

我在想,在ASIHTTPRequest完成上一個單獨的線程執行任務,所以我嘗試:

[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]; 

也什麼也沒做。

如何重新加載數據?我一直堅持這一段時間。

代碼: MainViewController.h

#import <UIKit/UIKit.h> 
@class ProductClass; 
@interface MainViewController : UITableViewController { 
ProductClass *item; 
} 
@property (nonatomic, retain) ProductClass *item; 
@end 

MainViewController.m

#import "MainViewController.h" 
#import "ASIHTTPRequest.h" 
#import "ProductClass.h" 

@implementation MainViewController 
@synthesize item; 

- (void) viewDidLoad { 
self.title = @"TableView Test"; 
self.tableView.allowsSelection = NO; 
self.item = [[ProductClass alloc] init]; 
    [self callURL]; 
} 

-(void)callURL { 
    NSURL *url = [NSURL URLWithString:@"http://urlgoeshere.com"]; 
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
[request setDelegate:self]; 
[request startAsynchronous]; 
} 


- (void)requestFinished:(ASIHTTPRequest *)request { 
//Grab the response 
NSString *responseString = [request responseString]; 
     //Put the result into the ProductClass item 
item.titleOfProduct = responseString; 

    //This line shows that self.tableview does NOT have an address of 0x0 
NSLog(@"%@\n", self.tableView); 

    //Problem line!!!!!! 
    [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]; 
    //The below lines also do nothing!!!! 
//[self.tableView reloadData]; 
//[[self tableView] reloadData]; 
} 

任何人有什麼想法?我處於全面虧損狀態。

乾杯, 佈雷特

回答

0

你不需要performSelectorOnMainThread - asihttprequest總是調用主線程的選擇。

你爲表設置了一個數據源嗎?你需要設置至少一些字段的數據源,請參閱:

http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITableViewDatasource_Protocol/Reference/Reference.html

至少的cellForRowAtIndexPath和numberOfRowsInSection。這裏有一個教程,可以幫助:

http://icodeblog.com/2008/08/08/iphone-programming-tutorial-populating-uitableview-with-an-nsarray/