开发者

Parsing JSON data to list on a UItableview for iPhone

开发者 https://www.devze.com 2023-04-02 02:37 出处:网络
I did try to bring JSON data to the iPhone and connected printed the incoming JSON data using NSLOG .was able to print and test the particular node.The i started using a for-loop to loop through the A

I did try to bring JSON data to the iPhone and connected printed the incoming JSON data using NSLOG .was able to print and test the particular node.The i started using a for-loop to loop through the Array of feed and display it in a UItableview and was not able to to do so . Tried a number of options but could not sort out this one .Please take a look at the code and let me know where and what i am doing wrong.I am still learning iPhone coding so any inout would be highly helpful.please find the code below.

#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];
    [responseDa开发者_如何学运维ta 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;

}


It's hard to say what's wrong without knowing what is happening, but here are a couple of observations.

  • It looks like you are parsing your response many times. Once in connectionDidFinishLoading to create the "exercises" array, and then again each time a cell is being rendered via tableView: cellForRowAtIndexPath:. I would assume that you are getting all the data you need when you first create the exercises array from JSON data. In that cases, tableView:cellForRowAtIndexPath: should just need to get the object out of the exercises array rather than dealing with the response data.
  • Also in here, I can't figure out what you are trying to do with the loop where you use the "player" - Seems like you are logging info but not doing anything with those player objects. In addition to not using them you seem to me leaking memory with these unreleased objects.

Based on the JSON you added in your comment, it appears that the exercises array will contain NSDictionary objects. That means your code to set the cell text is not right. Instead of

NSString *cellValue = [response objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;

you probably want something like

NSDictionary *exercise = [response objectAtIndex:indexPath.row];
cell.textLabel.text = [exercise valueForKey"player"];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号