[英]Parsing JSON data to list on a UItableview for iPhone
我確實嘗試將JSON數據帶到iPhone並使用NSLOG連接打印傳入的JSON數據。能夠打印和測試特定節點。我開始使用for循環遍歷feed數組並將其顯示在UItableview卻無法做到。 嘗試了多種選擇,但無法弄清這個選擇。請看一下代碼,讓我知道我在哪里做錯了。我仍在學習iPhone編碼,因此任何inout都將非常有幫助。請找到代碼下面。
#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,看來Exercises數組將包含NSDictionary對象。 這意味着您設置單元格文本的代碼不正確。 代替
NSString *cellValue = [response objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
你可能想要像
NSDictionary *exercise = [response objectAtIndex:indexPath.row];
cell.textLabel.text = [exercise valueForKey"player"];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.