0
SBJSON 프레임 워크 예제를 사용하여 JSON 피드를 사용하여 트위터에서 데이터를 다운로드 중입니다. 일단 다운로드가 끝나면 numberofrows가 0이됩니다. 데이터가 다운로드되기 전에 기다려야합니까, 아니면 코드에서 배열의 초기화가 누락 되었습니까?numberofrowsinsection 0 번
- (void)viewDidLoad {
[super viewDidLoad];
// Add the view controller's view to the window and display.
responseData = [[NSMutableData data] retain];
self.twitterArray = [NSMutableArray array];
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:@"http://search.twitter.com/search.json?q=mobtuts&rpp=5"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
[super viewWillAppear:animated];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];
NSDictionary *results = [responseString JSONValue];
self.twitterArray = [results objectForKey:@"results"];
[self.tableView reloadData]; // Correct way
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
NSLog(@"count : %d", [self.twitterArray count]); // Gets the count 0 here.
return [self.twitterArray count];
}
위와 같이 코드를 업데이트했습니다. 하지만 여전히 0이됩니다. – lifemoveson
'- (void) connectionDidFinishLoading : (NSURLConnection *) connection' 메소드의 끝에'[self.tableView reloadData];를 추가 했습니까? 처음에 0을 주어야하지만 다운로드와 구문 분석이 완료되면 실제 카운트를 반환해야합니다. –
내 대답은 Paul.s의 구체적인 설명으로 업데이트되었습니다. 그가 말했듯이, 메서드는 처음에 0 행을 인쇄하고 (반환하고) (nil이므로), 다운로드가 끝나면 다시 호출됩니다. –