首先看下总体界面:
Appdelegate文件代码:
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
ViewController *mainVC=[[ViewController alloc]init];
UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:mainVC];
self.window.rootViewController=nav;
[self.window makeKeyAndVisible];
return YES;
}
模型文件代码
#import <Foundation/Foundation.h>
@interface BookModel : NSObject
@property(nonatomic,copy)NSString *mBookName;
@property(nonatomic,copy)NSString *mBookPrice;
@property(nonatomic,copy)NSString *mPublisher;
@property(nonatomic,copy)NSString *mImageURL;
@end
主界面的.h文件代码
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
{
//声明数据视图
UITableView *_tableView;
//声明数据源对象
NSMutableArray *_arrayData;
//加载视图
UIBarButtonItem *_btnLoadData;
//编辑按钮
UIBarButtonItem *_btnEdit;
}
@end
主界面.m文件代码:
#import "ViewController.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "BookModel.h"
#define SCHEIGHT ([UIScreen mainScreen].bounds.size.height)//高度
#define IS_IPHONE_X (SCHEIGHT == 812.0f) ? YES : NO
#define Height_NavContentBar 44.0f
#define Height_NavBar (IS_IPHONE_X==YES)?88.0:64.0f
#define Height_TabBar (IS_IPHONE_X==YES)?83.0:49.0f
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title=@"SDWebImage的使用";
self.automaticallyAdjustsScrollViewInsets=NO;
_tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64) style:UITableViewStylePlain];
_tableView.dataSource=self;
_tableView.delegate=self;
//自动调节视图大小属性
_tableView.autoresizingMask=UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
_tableView.backgroundColor=[UIColor redColor];
[self.view addSubview:_tableView];
_arrayData=[[NSMutableArray alloc]init];
_btnLoadData=[[UIBarButtonItem alloc]initWithTitle:@"加载" style:UIBarButtonItemStylePlain target:self action:@selector(pressLoad)];
self.navigationItem.rightBarButtonItem=_btnLoadData;
}
-(void)pressLoad
{
[self loadDataFromNet];
}
-(void)loadDataFromNet
{
AFHTTPSessionManager *session=[AFHTTPSessionManager manager];
NSArray *arrG=[NSArray arrayWithObjects:@"iOS",@"Android",@"C++", nil];
static int counter=0;
counter++;
if (counter>=3) {
counter=0;
}
NSString *path=[NSString stringWithFormat:@"http://api.douban.com/book/subjects?q=%@&alt=json&apikey=01987f93c544bbfb04c97ebb4fce33f1",arrG[counter]];
[session GET:path parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"下载成功!");
if ([responseObject isKindOfClass:[NSDictionary class]]) {
[self parseData:responseObject];
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"下载失败!");
}];
}
-(void)parseData:(NSDictionary*)dicData
{
NSArray *arrEntry=[dicData objectForKey:@"entry"];
for (NSDictionary *dicBook in arrEntry) {
NSDictionary *dicTitle=[dicData objectForKey:@"title"];
NSString *strTitle=[dicTitle objectForKey:@"$t"];
BookModel *bModel=[[BookModel alloc]init];
//获取书籍的名字
bModel.mBookName=strTitle;
NSArray *arrLink=[dicBook objectForKey:@"link"];
for (NSDictionary *dicLink in arrLink) {
NSString *sValue=[dicLink objectForKey:@"@rel"];
if ([sValue isEqualToString:@"image"]) {
bModel.mImageURL=[dicLink objectForKey:@"@href"];
}
}
[_arrayData addObject:bModel];
[_tableView reloadData];
}
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _arrayData.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *strID=@"cell";
UITableViewCell *cell=[_tableView dequeueReusableCellWithIdentifier:strID];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:strID];
}
BookModel *bModel=_arrayData[indexPath.row];
cell.textLabel.text=bModel.mBookName;
//使用SDWebImage加载网络图片
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:bModel.mImageURL] placeholderImage:[UIImage imageNamed:@"super_light_80"]];
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
注意:
最后附上代码地址:传送门