简体   繁体   中英

JSON Data Are Not Parsed For All View

i am newBie in iOS Development.i make a Segmented Control For my Application and I want to Parsed Different WebService Data For Each Scrollview Contain For that i Use HMSegmented Controll and Set A scrollview For it like as

Here my Main view Controller Contain Code liken as

ViewController *latest=[[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];
    [latest.view setFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
    latest.index=0;
    [self addChildViewController:latest];
    [self.scrollView addSubview:latest.view];

    ViewController *latest2=[[ViewController alloc]initWithNibName:@"NavuViewController" bundle:nil];
    [latest2.view setFrame:CGRectMake(320, 0, self.view.frame.size.width, self.view.frame.size.height)];
    latest2.index=1;
    [self addChildViewController:latest2];
    [self.scrollView addSubview:latest2.view];

    ViewController * latest3 =[[ViewController alloc]initWithNibName:@"NavuViewController" bundle:nil];
    [latest3.view setFrame:CGRectMake(640, 0, self.view.frame.size.width, self.view.frame.size.height)];
    latest3.index=2;
    [self addChildViewController: latest3];
    [self.scrollView addSubview: latest3.view];

Then it Contain Different View As i want And in my View Controller.h file i Define index as int Variable

@property (assign) int index;

And Now I want to Fetch Data From Different Webservice Based on Index for that i Write a code in my ViewController.m file ViewDidLoad method i write a Code like as

-(void)view
{
[super viewWillAppear:animated];
[self.navuTable deselectRowAtIndexPath:self.navuTable.indexPathForSelectedRow animated:YES];
NSURL *url;
switch (self.index)
{
    case 0:
        url=[NSURL URLWithString:[NSString stringWithFormat:@"http://www.janvajevu.com/webservice/latest_post.php?page=%d",pageNum]];
        break;
    case 1:
    {
        NSString *urlString = @"http://www.janvajevu.com/webservice/categorylist.php?category=%E0%AA%9C%E0%AA%BE%E0%AA%A3%E0%AA%B5%E0%AA%BE%20%E0%AA%9C%E0%AB%87%E0%AA%B5%E0%AB%81%E0%AA%82&page=";

        url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%d",urlString,pageNum]];
    }
        break;
    case 2:

    {
        NSString *urlString = @"http://www.janvajevu.com/webservice/categorylist.php?category=%E0%AA%9F%E0%AB%87%E0%AA%B2%E0%AB%87%E0%AA%A8%E0%AB%8D%E0%AA%9F&page=";

        url= [NSURL URLWithString:[NSString stringWithFormat:@"%@%d",urlString,pageNum]];
    }
        break;
   default:
        break;
}
dispatch_async(kBgQueue, ^{
    data = [NSData dataWithContentsOfURL: url];
    [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
});

-(void)fetchedData:(NSData *)responsedata
{
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
spinner.frame = CGRectMake(0, 0, 320, 44);
self.navuTable.tableFooterView = spinner;
if (responsedata.length > 0)
{
    NSError* error;
    self.json= [NSJSONSerialization JSONObjectWithData:responsedata options:kNilOptions error:&error];
    if ([[_json objectForKey:@"data"] isKindOfClass:[NSArray class]])
    {
        NSArray *arr = (NSArray *)[_json objectForKey:@"data"];
        [self.navuArray addObjectsFromArray:arr];
        [self.navuTable reloadData];
        [spinner startAnimating];
        NSLog(@"Array %@",self.navuArray);
    }
}

Then it is Load Same Data for All Index means Only Fetch Data From First Index Url It is Mot Load Data For Different Index for Different Segmented Control please Give me Solution For it. thanks in advance.

Index stay at value of 0 all the time, it's not the first item , but it's the default value when you initialize it .

try to add the delegate of the Segment , see documentation and implement the ValueChanged : Delegate in which you can get the value of the selected index by yourSegment.selectedIndex ,, and that will be the value you should use to fetch different URLs.

otherwise the structure of your code is so week , you should use storyboard instead of nibs , maybe make a Model class with clean code then connect to your UI code . (MVC pattern)

hope that helps , post further data if you want more help . Good luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM