开发者

parsing json and showing in grid view

I m parsing a json url and showing it in a grid view.The output in the simulator looks like a grid of images similar to imagepicker.on pressing the image it got to navigate to a new view...the code works fine and i am able to receive the data in the console when the image is clicked.the problem is i m not able to navigate to the next view..cud u guys help me out

- (void)viewDidLoad {
    [super viewDidLoad];
    jsonurl=[NSURL URLWithString:@"http://www.1040communications.net/sheeba/stepheni/iphone/stephen.json"];
    jsonData=[[NSString alloc]initWithContentsOfURL:jsonurl];
    jsonArray = [jsonData JSONValue]; 
    items = [jsonArray objectForKey:@"items"];
    //  NSLog(@"hello:%@",items);
    story = [[NSMutableArray array]retain];
    media1 = [[NSMutableArray array]retain];

    for (NSDictionary *item in items )
    {
        [story addObject:[item objectForKey:@"title"]];
        [media1 addObject:[item objectForKey:@"media"]];    
    }

    UIScrollView *view = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    int row = 0;
    int column = 0;
    for(int i = 0; i < media1.count; i++) {
        NSString *mel=[media1 objectAtIndex:i];
        NSString *escapedURL = [mel stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
        UIImage *thumb = [[UIImage alloc]initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:escapedUR开发者_开发知识库L]]];
        button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(column*100+24, row*80+10, 64, 64);
        [button setImage:thumb forState:UIControlStateNormal];
        [button addTarget:self 
                   action:@selector(buttonClicked:) 
         forControlEvents:UIControlEventTouchUpInside];
        button.tag = i; 
        [view addSubview:button];

        if (column == 2) {
            column = 0;
            row++;
        } else {
            column++;
        }
    }

    [view setContentSize:CGSizeMake(320, (row+1) * 80 + 10)];
    self.view = view;
    [view release];
}

- (IBAction)buttonClicked:(id)sender
{
    UIImage *boy = [story objectAtIndex:button.tag];
    Detailview *detailview=[[Detailview alloc]init];
    [detailview initWithItem:boy];
    self.detailviewController=detailview;
    [self.navigationController pushViewController:self.detailviewController animated:YES];
}

parsing json and showing in grid view


Try this in your buttonClicked:

UIImage *boy = [story objectAtIndex:sender.tag];

You want the tag of sender, not button. button was set to the last one in your list when your buttons were created. sender is the ui object that caused this event.


hi looks like ur using old JSON parser. there is a much better parser for json.

thats JSONKIT.

It has just 2 files(.h and .m) and in just 2 lines of code u can get any data corresponding to the key. u dont hav to iterate through your items array. ur performance rate will definetly incr using JSONKit. if u tryin to use this let me i can post sample codes to parse json contents using jsonkit.

Secondly for the problem u have, u are putting the images in UIScrollview. ur uiscrollview doesnt have navigationcontrol to be pushed to the next view. either remove the scroll view and push from the rootviewcontroller or add navigationcontroller to uiscrollview. guess thats the prob. try it.

all the best.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜