来自照片UITableView的json未显示

时间:2014-09-04 09:15:13

标签: ios json parsing imageview tableview

我的问题: 在json中无法正确处理图像。每一行对应不正确的图像即将到来。有3种不同的尺寸。如何解析它们TableView的imageviewer'如何处理。谢谢。

截图:

http://i59.tinypic.com/f3wf47.png

http://i62.tinypic.com/e8va7c.png

我的代码:

#import "TableViewController.h"

@interface TableViewController (){

    NSMutableData *webData;
    NSURLConnection *connection;
    NSMutableArray *labelArray;
    NSMutableArray *imageArray;
}

@end

@implementation TableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSMutableArray *removeAllData = [NSMutableArray arrayWithObjects:imageArray,labelArray,nil];
    [removeAllData removeAllObjects];


    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
    labelArray = [[NSMutableArray alloc]init];
    imageArray = [[NSMutableArray alloc]init];

    NSString *strURL = @"https://itunes.apple.com/tr/rss/newapplications/limit=25/json";
    NSURL *url = [NSURL URLWithString:strURL];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    connection = [NSURLConnection connectionWithRequest:request delegate:self];

    if (connection) {
        webData = [[NSMutableData alloc]init];
    }

}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{

    [webData setLength:0];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

    [webData appendData:data];
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{

    NSLog(@"Error");
}

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

    NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:webData options:kNilOptions error:nil];
    NSDictionary *allFeedDictionary = [allDataDictionary objectForKey:@"feed"];

    NSArray *arrayOfEntry = [allFeedDictionary objectForKey:@"entry"];


    for (NSDictionary *diction in arrayOfEntry) {
        NSDictionary *title = [diction objectForKey:@"title"];
        NSDictionary *imImage = [diction objectForKey:@"im:image"];

        for (NSDictionary *imageDict in imImage) {
            NSDictionary *imageLabel = [imageDict objectForKey:@"label"];
            [imageArray addObject:imageLabel];
        }

        NSString *label = [title objectForKey:@"label"];
        [labelArray addObject:label];
    }
    [self.tableView reloadData];
    NSLog(@"%@",[imageArray objectAtIndex:0]);
}

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

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 1;
}

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

    return labelArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    //NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: [imageArray objectAtIndex:indexPath.row]]];
    NSData *imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:[imageArray objectAtIndex:indexPath.row]]];
    UIImage *image = [[UIImage alloc] initWithData:imageData];

    cell.textLabel.text = [labelArray objectAtIndex:indexPath.row];
    cell.imageView.image = image;

    return cell;
}

1 个答案:

答案 0 :(得分:1)

        for (NSDictionary *imageDict in imImage) {
            NSDictionary *imageLabel = [imageDict objectForKey:@"label"];
            [imageArray addObject:imageLabel];
break;
        }

OR

 [imageArray addObject: imImage[0][@"label"]];

只需在代码中替换它即可。您需要打破循环,因为您所需的大小图像始终位于第0个索引。