1
我有一个简单的JSON提要,我可以在控制台中解析并看到,但我似乎无法让它显示在我的表格视图中。我正在使用Xcode 5.0.2。我不确定为什么我可以在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输出 - 你还可以看到这个:news count:N? – plu
没有新闻计数是0,我可以看到connectionDidFinishLoading区域内的计数,但没有其他地方,它确实在那里得到计数。 –
您是否告诉mainTableView谁是委托和数据源? – plu