我在ViewController1中实现了一个UITableview并从JSON中检索数据。我想用Image,Tittle和Sub tittle显示单元格。我使用TableClass作为模型类
dispatch_async(dispatch_get_main_queue(), ^{
NSURLSession*session=[NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithURL:[NSURL URLWithString:@"https://itunes.apple.com/search?term=music"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"%@", json);
NSArray*entryarr=[json objectForKey:@"results"];
TableClass*tc=[[TableClass alloc]init];
for (NSDictionary*appDict in entryarr) {
NSString*str=[appDict objectForKey:@"artistName"];
[tc setTittle:str];
NSLog(@"artist Name=%@",tc.tittle);
//setting Subtittle
NSString*sub=[appDict objectForKey:@"country"];
[tc setSubtittle:sub];
NSLog(@"artist Name=%@",tc.subtittle);
NSString*imageStr=[appDict objectForKey:@"artworkUrl60"];
[tc setImage:imageStr];
NSURL*imageURL=[NSURL URLWithString:imageStr];
NSData*imageData=[[NSData alloc]initWithContentsOfURL:imageURL];
[self.imageArray addObject:imageData];
[_tableArray addObject:tc];
NSLog(@"%@ name of tittle",[_tableArray objectAtIndex:0]);
}
NSLog(@"%lu %lu %lu",(unsigned long)self.tableArray.count,(unsigned long)self.tableArray.count,(unsigned long)self.tableArray.count);
[self.tableView reloadData];
}];
[dataTask resume];
});
}
但现在我显示数据按
cell.textLabel.text=[[self.tableArray objectAtIndex:indexPath.row]valueForKey:@"_tittle"];
cell.detailTextLabel.text=[[self.tableArray objectAtIndex:indexPath.row]valueForKey:@"_subtittle"];
cell.imageView.image=[UIImage imageWithData:[[self.tableArray objectAtIndex:indexPath.row]valueForKey:@"_image"]];
但是我觉得这个方法没有优化,所以我想通过Model类本身显示Data我该怎么做......?
答案 0 :(得分:0)
TableClass.h
#import <Foundation/Foundation.h>
@interface TableClass : NSObject
@property(nonatomic,strong) NSString *title;
@property(nonatomic,strong) NSString *subtittle;
@property(nonatomic,strong) NSString *imageURL;
@property(nonatomic,strong) NSData *imageData;
@end
在ViewController.m
-(void)getData
{
NSURLSession*session=[NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithURL:[NSURL URLWithString:@"https://itunes.apple.com/search?term=music"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"%@", json);
NSArray*entryarr=[json objectForKey:@"results"];
self.tableArray=[[NSMutableArray alloc]init];
for (NSDictionary*appDict in entryarr)
{
TableClass *classModel=[[TableClass alloc]init];
classModel.title=[appDict objectForKey:@"artistName"];
classModel.subtittle=[appDict objectForKey:@"country"];
classModel.imageURL=[appDict objectForKey:@"artworkUrl60"];
classModel.imageData=[NSData dataWithContentsOfURL:[NSURL URLWithString:classModel.imageURL]];
[self.tableArray addObject:classModel];
}
NSLog(@"%lu %lu %lu",(unsigned long)self.tableArray.count,(unsigned long)self.tableArray.count,(unsigned long)self.tableArray.count);
dispatch_async(dispatch_get_main_queue(), ^{
//update UI in main thread.
[self.myTableView reloadData];
});
}];
[dataTask resume];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.tableArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier= @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:
UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
TableClass *tableclassModel=[self.tableArray objectAtIndex:indexPath.row];
cell.textLabel.text=tableclassModel.title;
cell.detailTextLabel.text=tableclassModel.subtittle;
[cell.imageView setImage:[UIImage imageWithData:tableclassModel.imageData]];
return cell;
}
在此示例中,您不需要额外的数组来存储图像。 如果您有任何疑问,请告诉我。