我的問題: 在json中無法正確處理圖像。每一行對應不正確的圖像即將到來。有3種不同的尺寸。如何解析它們TableView的imageviewer'如何處理。謝謝。json從照片UITableView中未顯示
截圖:
我的代碼:
#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;
}
你好,請問JSON字符串怎麼樣? – 2014-09-04 09:21:04
https://itunes.apple.com/tr/rss/newapplications/limit=25/json – 2014-09-04 09:21:45