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 回答
0

您的新闻数组是作为代码的局部变量。

在connectionDidFinishLoading中,请修改如下

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

所以它将是您在表格视图中访问的正确新闻数组。这对我来说可以。

于 2013-11-28T03:59:37.643 回答