這是我在這裏的第一篇文章,這是我的問題: 我試圖從REST API調用得到一些數據,並在一個UITableView然後顯示。
這正是我在做:
在viewDidLoad中:1)在這裏我初始化我的東西表中顯示(也就是空的開頭)2)的正裝表(數組與0行)和3)然後發出HTTP異步調用。
完成此操作我用HTTP響應來做我的東西,準備就緒時我在我的表上調用reloadData。這裏奇怪的發生。
numberOfRowsInSelection返回正確的行數 但是 tableView:cellForRowAtIndexPath:indexPath爲indexPath.row始終返回零! 因此沒有新行添加到表中。
- (void)doJSONRequest{
responseData = [[NSMutableData data] retain];
NSString *addr = [[NSString alloc] initWithFormat:@"http://localhost:8080/blabla/v1/items?count=10"];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:addr]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[addr release];
}
- (void)doJSONRequestWithURL:(NSString *)url{
responseData = [[NSMutableData data] retain];
NSString *addr = [[NSString alloc] initWithFormat:url];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:addr]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[addr release];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
//NSLog(@"[%@] connection:didReceiveResponse %@",[self class], response);
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
//NSLog(@"[%@] connection:didReceiveData %@",[self class], data);
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"[%@]",[NSString stringWithFormat:@"Connection failed: %@", [error description]]);
UIAlertView *alert = [[UIAlertView alloc] init];
[alert setTitle:@"NETWORK ERROR!"];
[alert setMessage:@"App will close"];
[alert setDelegate:self];
[alert addButtonWithTitle:@"Close"];
[alert show];
[alert release];
[alert show];
[alert release];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
//NSLog(@"[%@] connectionDidFinishLoading %@",[self class], connection);
[connection release];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];
NSDictionary *res = [NSDictionary dictionaryWithDictionary:[responseString JSONValue]];
NSDictionary *tmp = [res valueForKey:@"reviews"];
tmp = [tmp valueForKey:@"reviews"];
NSEnumerator *e = [tmp objectEnumerator];
NSDictionary *tmp_review;
int i=0;
while (tmp_review = [e nextObject]) {
//NSLog(@"tmp_review %@", tmp_review);
//NSLog(@"count %d", i++);
MyObject r = [... doing my staff...]
[reviews addObject: r];
[r release];
};
[reviewListTableView reloadData];
[responseString release];
}
- (void)viewDidLoad {
//- (void)initUI {
//review is the array where I put my data
//it is empty at the beginning
reviews = [[NSMutableArray alloc] initWithObjects:nil];
[super viewDidLoad];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [reviews count]
}
請張貼一些代碼=) – MishieMoo 2010-09-15 16:58:22
我已經添加了一些代碼,希望它會有所幫助! – 0m4r 2010-09-15 17:22:17
您確定您的結果數組已填充?另外,你可以在你設置單元的地方加入代碼嗎?看來這可能是問題所在。 – MishieMoo 2010-09-21 20:18:46