ViewController.m
//
// ViewController.m
// AFNetwork网络库4
//
// Created by chenshunyi on 2017/12/10.
// Copyright ? 2017年 house365. All rights reserved.
//#import "ViewController.h"
#import "AFNetworking.h"
#import "cityModel.h"
#import "cityTableViewCell.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>{
NSMutableArray*_cityLists;UITableView*_tableView;
}@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.CGSize screen=[[UIScreen mainScreen]bounds].size;
_tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, screen.width, screen.height) style:UITableViewStylePlain];_tableView.delegate=self;_tableView.dataSource=self;[self.view addSubview:_tableView];[self getNetworkData];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{return _cityLists.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{return 50;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{static NSString*strId=@"strId";cityTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:strId];if (!cell) {cell=[[cityTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:strId];}cityModel*model=[_cityLists objectAtIndex:indexPath.row];//把当前的cell的model传入到内部的cell进行使用[cell reloadCellData:model];return cell;
}//获取网络数据
-(void)getNetworkData{//创建http对象AFHTTPSessionManager*session=[AFHTTPSessionManager manager];//返回的数据类型session.responseSerializer.acceptableContentTypes=[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];//fas一个get请求[session GET:@"https://mtsapi.house365.com/secure/?method=newhouse.getGlobalProfile&city=bb&client=tf&channl=app&v=7.1.11&api_key=iPhone" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {NSLog(@"请求成功");if ([responseObject isKindOfClass:[NSDictionary class]]) {NSDictionary*diction=(NSDictionary*)responseObject;//dataNSDictionary*data=[diction objectForKey:@"data"];//cityListNSArray*cityList=[data objectForKey:@"cityList"];//创建一个城市的model数组_cityLists=[NSMutableArray array];//遍历每一个数据for (int i=0; i<cityList.count; i++) {NSDictionary*city=[cityList objectAtIndex:i];NSString*name=[city objectForKey:@"city_name"];NSLog(@"name=%@",name);cityModel*model=[[cityModel alloc]init];model.city_key=[city objectForKey:@"city_key"];model.city_name=[city objectForKey:@"city_name"];model.city_py=[city objectForKey:@"city_py"];model.city_zoom=[[city objectForKey:@"city_zoom"] integerValue];[_cityLists addObject:model];}if (_cityLists.count>0) {//刷新tableview[_tableView reloadData];}}} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {NSLog(@"err=%@",error);NSLog(@"请求失败");}];
}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}@end
cityTableViewCell.h
//
// cityTableViewCell.h
// AFNetwork网络库4
//
// Created by chenshunyi on 2017/12/10.
// Copyright ? 2017年 house365. All rights reserved.
//#import <UIKit/UIKit.h>#import "cityModel.h"@interface cityTableViewCell : UITableViewCell
//带参的方法,为了吧外面的model传到内部使用
-(void)reloadCellData:(cityModel*)model;@end
cityTableViewCell.m
//
// cityTableViewCell.m
// AFNetwork网络库4
//
// Created by chenshunyi on 2017/12/10.
// Copyright ? 2017年 house365. All rights reserved.
//#import "cityTableViewCell.h"@interface cityTableViewCell(){
UILabel*_keyLel;UILabel*_nameLel;UILabel*_pyLel;UILabel*_zoomLel;}
@end@implementation cityTableViewCell- (void)awakeFromNib {[super awakeFromNib];// Initialization code
}
-(void)reloadCellData:(cityModel*)model{if (!_nameLel) {_nameLel=[[UILabel alloc]initWithFrame:CGRectMake(15, 5, 100, 20)];_nameLel.font=[UIFont systemFontOfSize:15];_nameLel.textColor=[UIColor blackColor];[self addSubview:_nameLel];}_nameLel.text=model.city_name;if (!_pyLel) {_pyLel=[[UILabel alloc]initWithFrame:CGRectMake(15+100+10, 5, 100, 20)];_pyLel.font=[UIFont systemFontOfSize:15];_pyLel.textColor=[UIColor blackColor];[self addSubview:_pyLel];}_pyLel.text=model.city_py;if (!_keyLel) {_keyLel=[[UILabel alloc]initWithFrame:CGRectMake(15+100+10+100+10, 5, 100, 20)];_keyLel.font=[UIFont systemFontOfSize:15];_keyLel.textColor=[UIColor blueColor];[self addSubview:_keyLel];}_keyLel.text=model.city_key;if (!_zoomLel) {_zoomLel=[[UILabel alloc]initWithFrame:CGRectMake(15+100+10+100+10, 5+20+5, 100, 20)];_zoomLel.font=[UIFont systemFontOfSize:15];_zoomLel.textColor=[UIColor yellowColor];[self addSubview:_zoomLel];}_zoomLel.text=[NSString stringWithFormat:@"%ld",model.city_zoom];
}- (void)setSelected:(BOOL)selected animated:(BOOL)animated {[super setSelected:selected animated:animated];// Configure the view for the selected state
}@end
cityModel.h
//
// cityModel.h
// AFNetwork网络库4
//
// Created by chenshunyi on 2017/12/10.
// Copyright ? 2017年 house365. All rights reserved.
//#import <Foundation/Foundation.h>@interface cityModel : NSObject@property(nonatomic,copy)NSString*city_key;
@property(nonatomic,copy)NSString*city_name;
@property(nonatomic,copy)NSString*city_py;
@property(nonatomic,assign)NSInteger city_zoom;@end
cityModel.m
//
// cityModel.m
// AFNetwork网络库4
//
// Created by chenshunyi on 2017/12/10.
// Copyright ? 2017年 house365. All rights reserved.
//#import "cityModel.h"@implementation cityModel@end