本節(jié)主題(簡(jiǎn)單的網(wǎng)絡(luò)部分)
- 源碼地址在文章末尾
-
達(dá)成效果
效果圖 注:本文API使用HaoService數(shù)據(jù)平臺(tái)
前言
- 廢話不說了,簡(jiǎn)單來說,移動(dòng)網(wǎng)絡(luò)很重要,下面讓我們來實(shí)現(xiàn)一個(gè)簡(jiǎn)單的Demo:對(duì)網(wǎng)絡(luò)數(shù)據(jù)的請(qǐng)求,并接收與展示吧。
注意:是簡(jiǎn)單的!很簡(jiǎn)單的!
準(zhǔn)備工作
1.文件目錄結(jié)構(gòu)示圖(按照MVC分層)
文件目錄結(jié)構(gòu)圖/自定義Cell
Controller: CYXOneViewController
Model: CYXMenu
View: CYXCell
2.使用cocoapods集成第三方框架
注:這里就直接使用cocoapods插件安裝第三方框架了
第三方框架
- 這里要使用到的三方框架包括AFNetworking、MJExtension、SDWebImage
- 框架用途簡(jiǎn)介
- AFNetworking:用于發(fā)送網(wǎng)絡(luò)請(qǐng)求
- MJExtension:用于把網(wǎng)絡(luò)返回的JSON格式數(shù)據(jù)轉(zhuǎn)換為模型屬性
- SDWebImage:用于下載網(wǎng)絡(luò)圖片
基本思路簡(jiǎn)述
- 1.在
CYXOneViewController
中使用AFNetworking
發(fā)送GET
請(qǐng)求,得到服務(wù)器返回的JSON
格式的數(shù)據(jù) - 2.使用
MJExtension
把JSON
格式的數(shù)據(jù)轉(zhuǎn)為模型CYXMenu
對(duì)應(yīng)的屬性 - 3.在
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
方法內(nèi)根據(jù)索引indexPath.row
把每一行Cell的Model屬性傳遞給自定義Cell(CYXCell
) - 4.在自定義Cell(
CYXCell
)內(nèi)接收模型數(shù)據(jù)并初始化Cell的內(nèi)部控件 - 5.不要忘記在AFN框架內(nèi)的GET請(qǐng)求成功調(diào)用的block中刷新TableView的數(shù)據(jù)
詳細(xì)實(shí)現(xiàn) 上代碼
- 第一步:設(shè)計(jì)模型屬性(CYXMenu.h),這里的屬性名根據(jù)API文檔設(shè)計(jì),本Demo只是使用了其中幾個(gè)簡(jiǎn)單的
#import <Foundation/Foundation.h>
@interface CYXMenu : NSObject
/** 圖片 */
@property (copy, nonatomic) NSString * albums;
/** 標(biāo)題 */
@property (nonatomic,copy) NSString *title;
/** 材料 */
@property (nonatomic,copy) NSString *ingredients;
@end
- 第二步:自定義cell(這里使用Xib了)
- 2.1 拖控件到Xib中(見文件目錄結(jié)構(gòu)圖)
- 2.2 在
CYXCell.h
中定義一個(gè)模型屬性,用于供外界(CYXOneViewController)訪問并傳遞模型進(jìn)來。
#import <UIKit/UIKit.h> @class CYXMenu; @interface CYXCell : UITableViewCell /** 菜單模型 */ @property (strong, nonatomic) CYXMenu * menu; @end
- 2.3 在
CYXCell.m
中實(shí)現(xiàn)menu屬性的- (void)setMenu:(CYXMenu *)menu
方法,將取到的menu模型值給cell自身的控件賦值
由于這里需要通過URL下載網(wǎng)絡(luò)圖片,使用到了SDWebImage框架
#import "CYXCell.h" #import "CYXMenu.h" #import <UIImageView+WebCache.h> @interface CYXCell () @property (weak, nonatomic) IBOutlet UIImageView *albumsImageView; @property (weak, nonatomic) IBOutlet UILabel *titleLable; @property (weak, nonatomic) IBOutlet UILabel *ingredientsLabel; @end @implementation CYXCell - (void)setMenu:(CYXMenu *)menu{ _menu = menu; // 利用SDWebImage框架加載圖片資源 [self.albumsImageView sd_setImageWithURL:[NSURL URLWithString:menu.albums]]; // 設(shè)置標(biāo)題 self.titleLable.text = menu.title; // 設(shè)置材料數(shù)據(jù) self.ingredientsLabel.text = menu.ingredients; } @end
- 第三步,到這里我們需要在
CYXOneViewController.m
內(nèi)實(shí)現(xiàn)網(wǎng)絡(luò)數(shù)據(jù)請(qǐng)求
/JSON數(shù)據(jù)轉(zhuǎn)模型
/給自定義cell傳遞模型數(shù)據(jù)
的操作
#import "CYXOneViewController.h"
#import "CYXCell.h"
#import "CYXMenu.h"
#import <AFNetworking.h>
#import <MJExtension.h>
@interface CYXOneViewController ()
/** 存放數(shù)據(jù)模型的數(shù)組 */
@property (strong, nonatomic) NSMutableArray * menus;
@end
@implementation CYXOneViewController
#pragma mark - 全局常量
// 發(fā)送請(qǐng)求URL
static NSString * const CYXRequestURL = @"http://apis.haoservice.com/lifeservice/cook/query?";
// 重用cell標(biāo)識(shí)符
static NSString * const CYXCellID = @"cell";
#pragma mark - life cycle 生命周期方法
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.rowHeight = 90;
// 注冊(cè)重用Cell
[self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([CYXCell class]) bundle:nil] forCellReuseIdentifier:CYXCellID];
// 調(diào)用加載數(shù)據(jù)方法
[self loadData];
self.view.backgroundColor = [UIColor whiteColor];
}
#pragma mark - private methods 私有方法
/**
* 發(fā)送請(qǐng)求并獲取數(shù)據(jù)方法
*/
- (void)loadData{
// 請(qǐng)求參數(shù)(根據(jù)接口文檔編寫)
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"menu"] = @"西紅柿";
params[@"pn"] = @"1";
params[@"rn"] = @"20";
params[@"key"] = @"2ba215a3f83b4b898d0f6fdca4e16c7c";
// 在AFN的block內(nèi)使用,防止造成循環(huán)引用
__weak typeof(self) weakSelf = self;
[[AFHTTPSessionManager manager] GET:CYXRequestURL parameters:params success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject) {
NSLog(@"請(qǐng)求成功");
// 利用MJExtension框架進(jìn)行字典轉(zhuǎn)模型
weakSelf.menus = [CYXMenu objectArrayWithKeyValuesArray:responseObject[@"result"]];
// 刷新數(shù)據(jù)(若不刷新數(shù)據(jù)會(huì)顯示不出)
[weakSelf.tableView reloadData];
} failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
NSLog(@"請(qǐng)求失敗 原因:%@",error);
}];
}
#pragma mark - UITableviewDatasource 數(shù)據(jù)源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.menus.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
CYXCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
cell.menu = self.menus[indexPath.row];
return cell;
}
#pragma mark - UITableviewDelegate 代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// 點(diǎn)擊了第indexPath.row行Cell所做的操作
}
@end
- 到這里只是簡(jiǎn)單實(shí)現(xiàn)了網(wǎng)絡(luò)數(shù)據(jù)的請(qǐng)求,還有很多細(xì)節(jié),例如下拉/上拉刷新,、cell的點(diǎn)擊事件等等,有時(shí)間再討論了。
- 附:源碼github地址