Ten minutes to build the mainstream framework of simple network _ part (OC)

Topic of this section (simple network section)

  • The source address is at the end of the article
  • Achieve effect
    Ten minutes to build the mainstream framework of simple network _ part (OC)
    renderings
  • Note: This article uses the HaoService data platform for API

Preface

  • Nonsense, simply speaking, the mobile network is very important, let us implement a simple Demo: request for network data, and receive and display it. Note: it’s simple! Very simple!

Dead work

1. file directory structure diagram (in accordance with MVC layered)

Ten minutes to build the mainstream framework of simple network _ part (OC)
file directory structure / custom Cell
  • Controller: CYXOneViewController
  • Model: CYXMenu
  • View: CYXCell

2. use cocoapods to integrate the third party framework

  • Note: Here you install the third party framework directly using the cocoapods plug-in
Ten minutes to build the mainstream framework of simple network _ part (OC)
third party framework
  • The three party framework to be used here includes AFNetworking, MJExtension, and SDWebImage
  • Framework usage profile AFNetworking: used to send network request MJExtension: used to convert network returned JSON formatted data to model properties SDWebImage: used to download network pictures

Brief introduction of basic train of thought

  • 1. in CYXOneViewController, use AFNetworking to send GET requests and get the data returned by the server in JSON format
  • 2. use MJExtension to convert data in JSON format to attributes corresponding to model CYXMenu
  • In 3. – (UITableViewCell) tableView: (UITableView * tableView) cellForRowAtIndexPath: (NSIndexPath * indexPath); method according to the Model property of the indexPath.row index of each row of the Cell passed to the custom Cell (CYXCell)
  • 4. receive the model data in the custom Cell (CYXCell) and initialize the Cell’s internal controls
  • 5. don’t forget to refresh TableView data in the block that is successfully invoked by the GET request within the AFN framework

Detailed implementation of the code

  • The first step is to design model attributes (CYXMenu.h), where the property names are designed according to the API document. This Demo uses only a few simple ones
#import < Foundation/Foundation.h> @interface; CYXMenu: NSObject / * * * / @property images (copy, nonatomic) NSString * albums; / * * * Title @property (nonatomic, copy) NSString *title; / * * * / @property materials (nonatomic, copy) NSString *ingredients; @end
  • The second step: Custom cell (here Xib) 2.1 drag controls to Xib (see Figure 2.2 in the file directory structure) CYXCell.h defines a model attribute, used for external access and transfer model (CYXOneViewController) in. #import < UIKit/UIKit.h&gt @class; CYXMenu @interface; CYXCell: UITableViewCell / * * * / @property menu model (strong, nonatomic) CYXMenu * menu; @end 2.3 in CYXCell.m menu – (void) setMenu: attributes (CYXMenu *) menu method, the menu model to get the value to control the assignment
    cell because of its own need here through the URL network to download pictures, using the framework of 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 (void – CYXCell; (CYXMenu * setMenu:) Menu{_menu = menu); / / use SDWebImage framework [self.albumsImageView sd_setImageWithURL:[NSURL URLWithString:menu.albums]] to load the picture resources; / / set the title of self.titleLable.text = menu.title; self.ingredientsLabel.text = menu.ingredients / / data set;} @end
  • The third step, here, we need to implement network data requests in CYXOneViewController.m, /JSON data transfer model / to transfer the model data to the custom cell
#import "CYXOneViewController.h" #import "CYXCell.h" #import "CYXMenu.h" #import < AFNetworking.h> #import; < MJExtension.h> @interface (CYXOneViewController) / * * storage data model array (strong, nonatomic) @property * NSMutableArray * menus; @end @implementation CYXOneViewController #pragma mark - URL static / / global constants request NSString * const = CYXRequestURL @ http://apis.haoservice.com/lifeservice/cook/query? "; / / cell static NSString * const reuse identifier @ CYXCellID =" cell "; #pragma mark - life cycle life cycle method - (void) viewDidLoad {[super viewDidLoad]; self.tableView.rowHeight = 90; / / register reuse Cell [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass ([CYXCell class]) bundle:nil] forCellReuseIdentifier:CYXCellID]; / / call load data method [self loadData]; self.view.backgroundColor = [UIColor whiteColor] #pragma mark private methods;} / * * * private method to send the request and method to obtain data * / - (void) loadData{/ / (written request parameters according to the interface document) NSMutableDictionary *params = [NSMutableDictionary dictionary]; params[@ "menu"] = @ "tomato"; params[@ "PN"] = "@ 1"; params[@ "RN"] = "@ 20"; params[@ "key"] = "@ 2ba215a3f83b4b898d0f6fdca4e16c7c"; / / use in AFN block, to avoid circular references __weak typeof (self) weakSelf = self; [[AFHTTPSessionManager manager] GET:CYXRequestURL parameters:params success:^ (NSURLSessionDataTask * _Nonnull task, ID _Nonnull responseObject (NSLog) {@ "request"); / / dictionary turn model weakSelf.menus = "result" [CYXMenu objectArrayWithKeyValuesArray:responseObject[@ "using the MJExtension framework; / / refresh the data (if not refresh the data will not show) [weakSelf.tableView reloadData];} failure:^ (NSURLSessionDataTask * _Nonnull task NSError * _Nonnull error) {NSLog (@" request failure:% @ ", error);}]; #pragma} mark - UITableviewDatasource data source methods - (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: agent method (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath * indexPath{) / / click on the line indexPath.row Cell work @end}
  • Here is a simple implementation of the network data request, there are many details, such as drop-down / up and refresh, cell click event, etc., there is time to discuss.
  • Attached: source GitHub address