【发布时间】:2011-11-08 21:05:34
【问题描述】:
我确实尝试将 JSON 数据带到 iPhone 并使用 NSLOG 连接打印传入的 JSON 数据。能够打印和测试特定节点。我开始使用 for-loop 循环遍历提要和显示数组它在 UItableview 中并且无法这样做。尝试了许多选项,但无法解决这个问题。请查看代码,让我知道我在哪里以及做错了什么。我仍在学习 iPhone 编码,因此任何输入输出都会非常有帮助。请找到代码下面。
#import "RootViewController.h"
#import "SBJson.h"
#import "JSON.h"
@implementation RootViewController
NSMutableArray *listOfStates;
NSArray *exercises;
NSMutableData *responseData;
- (void)viewDidLoad
{
//---initialize the array---
listOfStates = [[NSMutableArray alloc] init];
responseData = [[NSMutableData data] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost/~xxxx/MyWorks/index.php?playerid=0"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self ];
//NSURL *url = [NSURL URLWithString:@""];
[super viewDidLoad];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { NSLog(@"didReceiveResponse"); [responseData setLength:0]; }
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data]; }
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"Connection failed: %@", [error description]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];
NSDictionary *dictionary = [responseString JSONValue];
NSArray *response = [dictionary valueForKey:@"feed"];
NSLog(@"Here is the title of the feed: %@", [response valueForKey:@"video"]);
exercises = [[NSArray alloc] initWithArray:response];
[[self tableView] reloadData];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [exercises count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];
NSDictionary *dictionary = [responseString JSONValue];
NSArray *response = [dictionary valueForKey:@"feed"];
//create a cell
UITableViewCell *cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"cell"];
// fill it with contnets
int ndx;
for (ndx = 0; ndx <dictionary.count; ndx++) {
NSDictionary *player = (NSDictionary *)[dictionary objectAtIndex:ndx];
NSLog(@"Player: %@", [player valueForKey:@"player"]);
}
NSString *cellValue = [player objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
// return it
return cell;
}
【问题讨论】:
-
你能再具体一点吗?究竟是什么不工作?另外,您收到的 JSON 是什么样的?
-
@Tim 当我尝试将 JSON 数据的值发布到 cellForRowAtIndexPath 函数中的 cellValue 中时,它失败了。请在此处找到 JOSN 数据 {"feed": [{"id":"1","player":"player 1","video":"big_hit.mp3"},{"id":"1", "player":"player 2","video":"tribute .mp3"},{"id":"2","player":"player 3","video":"Interview.mp3"}]}
标签: iphone json uitableview xcode4