콘솔에서 구문 분석하고 볼 수있는 간단한 JSON 피드가 있지만 테이블 뷰에 표시 할 수없는 것 같습니다. Xcode 5.0.2를 사용하고 있습니다. 나는 콘솔에서 echo되었을 때 왜 for 루프에서 볼 수 있는지 모르지만 테이블 뷰에서는 볼 수 없다.json 요소가 테이블 뷰로 전달되지 않음
참고로 xcode 및 목표 C를 작성하는 데 아주 신형이며이 video tutorial을 가이드로 사용하고 있습니다.
내 JSON은 다음과 같습니다
[ { "first_name": "Bill" }, { "first_name": "Ted" }, { "first_name": "George" } etc... ]
PhotoTableViewController.h을
#import #import "DisplayViewController.h" @interface PhotosTableViewController : UITableViewController { IBOutlet UITableView *mainTableView; NSArray *news; NSMutableData *data; } @end
PhotoTableViewController.m
@interface PhotosTableViewController() { NSMutableArray *photos; NSArray *_locations; } - (void)viewDidLoad { NSURL *url = [NSURL URLWithString:@"http://feedurl.json"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; [[NSURLConnection alloc] initWithRequest:request delegate:self]; } // some code left out for brevity - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. //return photos.count; NSLog(@"news count: %lu", news.count); return news.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; cell.textLabel.text = [[photos objectAtIndex:indexPath.row] objectForKey:@"first_name"]; NSLog(@"anything: %@", [[photos objectAtIndex:indexPath.row] objectForKey:@"first_name"]); return cell; } - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { data = [[NSMutableData alloc] init]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData { [data appendData:theData]; } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; news = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; NSDictionary *first_name; for (int i=0; i (cant put less than sign here breaks code view) [news count] i++) { first_name = [news objectAtIndex:i]; NSLog(@"Statuses: %@", [first_name objectForKey:@"first_name"]); } [mainTableView reloadData]; } -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"The download could not complete - please make sure you're connected to the internet." delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil]; [errorView show]; [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; }
언뜻보기에 코드가 정상적으로 보입니다. 당신은 콘솔에서 NSLog 출력을 볼 수 있다고 말합니다 - 당신도 이것을 볼 수 있습니까? 뉴스 카운트 : N? – plu
아니 뉴스 수는 0입니다, 나는 connectionDidFinishLoading 영역 내부의 카운트를 볼 수 있지만 다른 곳에서는 카운트를 얻지 못합니다. –
mainTableView는 델리게이트와 데이터 소스를 누구에게 말 했나요? – plu