2014-09-04 78 views
0

我的問題: 在json中無法正確處理圖像。每一行對應不正確的圖像即將到來。有3種不同的尺寸。如何解析它們TableView的imageviewer'如何處理。謝謝。json從照片UITableView中未顯示

截圖:

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; 
} 
+0

你好,請問JSON字符串怎麼樣? – 2014-09-04 09:21:04

+0

https://itunes.apple.com/tr/rss/newapplications/limit=25/json – 2014-09-04 09:21:45

回答

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

OR

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

只需在您的代碼中將其替換即可。您需要打破循環,因爲您所需的大小圖像始終位於第0個索引處。

+0

謝謝。我嘗試着。 – 2014-09-04 09:32:35

+0

成功。非常感謝你。 :)我一直在處理這個2天:) – 2014-09-04 09:35:39