Map request data to model class

Source: Internet
Author: User

Add some third-party class libraries afnetworking, Jsonkit-nowarning, Ocmapper, Sdwebimage

#import "AFNetworking.h"

#import "JSONKit.h"

#import "FirstModel.h"

#import "Nsobject+objectmapper.h"

#import "SDImageCache.h"

#import "Uiimageview+webcache.h"

@interface Viewcontroller () <UITableViewDataSource,UITableViewDelegate>

{

Nsarray *_hotarray;

}

@end

@implementation Viewcontroller

-(void) Viewdidload {

[Super Viewdidload];

UITableView *tableview =[[uitableview alloc]initwithframe:cgrectmake (0, Self.view.frame.size.width, self.view.frame.size.height) Style:uitableviewstyleplain];

Tableview.delegate =self;

Tableview.datasource =self;

[Self.view Addsubview:tableview];

Tableview.tablefooterview=[uiview new];

NSString *urlstr [email protected] "http://www.022eh.com/api/return_sub";

Nsdictionary *dic [Email protected]{@ "PageSize": @ "3" @ "index": @ "1"};

Afhttprequestoperationmanager *manager =[afhttprequestoperationmanager Manager];

[Manager post:urlstr Parameters:dic success:^ (afhttprequestoperation *operation, id responseobject) {

NSLog (@ "0000000=%@", operation.responsestring);

Nsdictionary *resat =[operation.responsedata Objectfromjsondata];

Nsdictionary *data =[resat objectforkey:@ "List"];

_hotarray =[firstmodel Objectfromdictionary:data];

[TableView Reloaddata];

} failure:^ (Afhttprequestoperation *operation, Nserror *error) {

}];

}

-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (Nsinteger) section{

return _hotarray.count;

}

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath

{

UITableViewCell *cell =[tableview dequeuereusablecellwithidentifier:@ "Cell"];

if (!cell) {

Cell =[[uitableviewcell alloc]initwithstyle:uitableviewcellstyledefault reuseidentifier:@ "Cell"];

}

Firstmodel *first =[_hotarray ObjectAtIndex:indexPath.row];

Cell.textlabel.text=first.name;

[Cell.imageview sd_setimagewithurl:[nsurl urlwithstring:[nsstring stringwithformat:@ "http://www.022eh.com%@", FIRST.IMG]] placeholderimage:[uiimage imagenamed:@ "908fa0ec08fa513df6da05f9386d55fbb2fbd9a1.jpg"];

return cell;

}

-(void) didreceivememorywarning {

[Super didreceivememorywarning];

}

@end

Add in Firstmodel.h

#import <Foundation/Foundation.h>

@interface Firstmodel:nsobject

@property (Nonatomic,strong) nsstring *name;//to match the given number of letters

@property (nonatomic) NSString *id;

@property (Nonatomic,strong) NSString *img;

@end

Map request data to model class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.