0

私のコードでは、このリンクhttp://www.youtube.com/watch?v=RJZcD3hfs3kと成功の指示に従って
いますが、複数の JSON に変更したくて失敗しました (ログを印刷した場合、実行中です)。

私は次のように変更します:

- (void)viewDidLoad  
- (void)connectionDidFinishLoading:(NSURLConnection *)connection  

これは私の変更されたコード(ViewController.m)です:

#import "ViewController.h"
#import "DetailViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"News";

    //[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    //before
    //NSURL *url = [NSURL URLWithString:@"http://zacandcatie.com/YouTube/json.php"];

    //after
    NSURL *url = [NSURL URLWithString:@"http://service.berisiknews.com/article/byAll/0/3"];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];

    // Do any additional setup after loading the view, typically from a nib.
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    data = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData
{
    [data appendData:theData];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    //before 
    //news = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
    //[mainTableView reloadData];
    //ßNSLog(@"array %@", news);

    //after
    NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error: nil];
    NSArray *news = [jsonArray valueForKeyPath:@"data"];
    [mainTableView reloadData];
    NSLog(@"array %@", news);
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"The download could not complete please make sure you're connected to either 3G or Wi-Fi" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles: nil];
    [errorView show];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

- (int)numberINSectionsInTableView: (UITableView *)tableView
{
    return 1;
}

- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [news count];
    //return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MainCell"];
    }

    cell.textLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"title"];
    //cell.detailTextLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"date_string"];
    cell.detailTextLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"category_name"];
    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
    detailViewController.title = [[news objectAtIndex:indexPath.row] objectForKey:@"title"];
    detailViewController.newsArticle = [news objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:detailViewController animated:YES];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

何か助けはありますか?

4

2 に答える 2

0
@interface AlbumViewController ()

@end

@implementation AlbumViewController

- (void)viewDidLoad

{

    [super viewDidLoad];
    //navigation Controller
    [self.navigationController setNavigationBarHidden:NO];
    self.title=@"Album List";

    //json data parsing
    NSURL *url = [NSURL URLWithString:@".......your Link......"];

    NSURLRequest *urlrequest = [[NSURLRequest alloc]initWithURL:url];

    NSData *data = [NSURLConnection sendSynchronousRequest:urlrequest returningResponse:nil error:nil];

    dict_data = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    self.ary_data = [dict_data objectForKey:@"album"];

    NSLog(@"%@",self.ary_data);

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return [ary_data count];

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    UITableViewCell *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    NSMutableDictionary *dic= [[ary_data objectAtIndex:indexPath.row] mutableCopy];
    AlbumCellController *albumCell = [[AlbumCellController alloc]init];

    [cell.contentView addSubview:albumCell.view];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                       albumCell.Img_album.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[dic objectForKey:@"cover_photo"]]]];
    });
    albumCell.lbl_name.text = [dic objectForKey:@"title"];
    albumCell.lbl_releasedate.text = [dic objectForKey:@"release_date"];

    NSString *SongNo=[dic objectForKey:@"no_of_songs"];
    NSLog(@"%@",SongNo);
    //albumCell.lbl_songno.text=SongNo;

    return cell;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSMutableDictionary *dic= [[ary_data objectAtIndex:indexPath.row] mutableCopy];
    SongsViewController *songList = [[SongsViewController alloc]init];
    songList.imgURL = [dic objectForKey:@"cover_photo"];
    songList.Album_id = [dic objectForKey:@"album_id"];
    [self.navigationController pushViewController:songList animated:YES];

}
于 2014-04-16T06:50:08.943 に答える