首先导入三个文件
SDWebimage
MJExtension
AFNetworking
创建MVC
在M里创建一个继承NSobject的类
在m.h里
@property(nonatomic,strong)NSString * title;//主标题
@property(nonatomic,strong)NSString * date;//日期标题
@property(nonatomic,strong)NSString * thumbnail_pic_s;//图片
@property(nonatomic,strong)NSString * thumbnail_pic_s02;//图片
@property(nonatomic,strong)NSString * thumbnail_pic_s03;//图片
@property(nonatomic,strong)NSString * author_name;//
在V里创建一个继承UITableViewCell的类
在v.h里
@class Model;//声明一个类
@interface YouTableViewCell : UITableViewCell
@property(nonatomic,strong)Model * m;
在v.m里
导入#import “Model.h”
#import “UIImageView+WebCache.h”
@property(nonatomic,strong)UILabel * zhuTitle;//主标题
@property(nonatomic,strong)UILabel * DateTitle;//时间标题
@property(nonatomic,strong)UIImageView * imgv1;//图片一
@property(nonatomic,strong)UIImageView * imgv2;//图片二
@property(nonatomic,strong)UIImageView * imgv3;//图片三
@property(nonatomic,strong)UILabel * rightTitle;//小标题
//初始化方法 添加所有的子控件
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if(self=[super initWithStyle:style reuseIdentifier:reuseIdentifier])
{
[self addSubview:self.zhuTitle];
[self addSubview:self.DateTitle];
[self addSubview:self.imgv1];
[self addSubview:self.imgv2];
[self addSubview:self.imgv3];
[self addSubview:self.rightTitle];
}
return self;
}
//懒加载
-(UILabel * )zhuTitle
{
if(!_zhuTitle)
{
_zhuTitle=[[UILabel alloc]init];
_zhuTitle.font=[UIFont systemFontOfSize:17];
_zhuTitle.numberOfLines=0;
}
return _zhuTitle;
}
-(UILabel * )DateTitle
{
if(!_DateTitle)
{
_DateTitle=[[UILabel alloc]init];
_DateTitle.font=[UIFont systemFontOfSize:12];
_DateTitle.textColor=[UIColor lightGrayColor];
}
return _DateTitle;
}
-(UIImageView * )imgv1
{
if(!_imgv1)
{
_imgv1=[[UIImageView alloc]init];
}
return _imgv1;
}
-(UIImageView * )imgv2
{
if(!_imgv2)
{
_imgv2=[[UIImageView alloc]init];
}
return _imgv2;
}
-(UIImageView * )imgv3
{
if(!_imgv3)
{
_imgv3=[[UIImageView alloc]init];
}
return _imgv3;
}
-(UILabel * )rightTitle
{
if(!_rightTitle)
{
_rightTitle=[[UILabel alloc]init];
_rightTitle.font=[UIFont systemFontOfSize:15];
_rightTitle.textColor=[UIColor lightGrayColor];
_rightTitle.textAlignment=UITextAlignmentRight;
}
return _rightTitle;
}
//布局子控件 设置子控件的位置
-(void)layoutSubviews
{
[super layoutSubviews];
self.zhuTitle.frame=CGRectMake(5, 10, 300, 80);
self.DateTitle.frame=CGRectMake(310, 15, 150, 42);
self.imgv1.frame=CGRectMake(5, 85, 80, 80);
self.imgv2.frame=CGRectMake((self.frame.size.width-80*3)/2-5+80, 85, 80, 80);
self.imgv3.frame=CGRectMake(((self.frame.size.width-80*3)/2-5)*2+80*2, 85, 80, 80);
self.rightTitle.frame=CGRectMake((self.frame.size.width-80*3)+80*2-50, 168, 125, 30);
}
//Model的set方法
-(void)setM:(Model *)m
{
_m=m;
self.zhuTitle.text=m.title;
self.DateTitle.text=m.date;
self.rightTitle.text=m.author_name;
//把网络中的图片路径用第三方解析出来
[self.imgv1 sd_setImageWithURL:[NSURL URLWithString:m.thumbnail_pic_s] placeholderImage:[UIImage imageNamed:@""]];
[self.imgv2 sd_setImageWithURL:[NSURL URLWithString:m.thumbnail_pic_s02] placeholderImage:[UIImage imageNamed:@""]];
[self.imgv3 sd_setImageWithURL:[NSURL URLWithString:m.thumbnail_pic_s03] placeholderImage:[UIImage imageNamed:@""]];
}
在AppDelegate.m里
首先导入
#import “ViewController.h”
之后
self.window.rootViewController=[[UINavigationController alloc]initWithRootViewController:[ViewController new]];
在v.m里
首先导入
#import “AFNetworking/AFNetworking.h”
#import “MJExtension/MJExtension.h”
#import “Modle/Model.h”
#import “UIImageView+WebCache.h”
#import “YouTableViewCell.h”
之后
<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)NSMutableArray * dataArr;
@property(nonatomic,strong)UITableView * tbv;
-
(void)viewDidLoad {
[super viewDidLoad];
//设置导航条颜色
self.navigationController.navigationBar.barTintColor=[UIColor redColor];//设置搜索条
UISearchBar * searchBar=[[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 300, 40)];//将搜索条添加到导航条
self.navigationItem.titleView=searchBar;UIImageView * imgv=[[UIImageView alloc]initWithFrame:CGRectMake(350, 15, 60, 40)];
imgv.image=[UIImage imageNamed:@“find_Highlighted@2x.png”];
[searchBar addSubview:imgv];
//添加到主视图
self.tbv=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];//设置代理和数据源
self.tbv.delegate=self;self.tbv.dataSource=self;
[self.view addSubview:self.tbv];
self.dataArr=[NSMutableArray array];
//创建网络请求管理者对象
AFHTTPSessionManager * manager=[AFHTTPSessionManager manager];NSDictionary * dic =@{@“type”?“top”,@“key”?“a6d29bb03f7ccb465b9c21ace3ba79a3”};
[manager GET:@“http://v.juhe.cn/toutiao/index” parameters:dic success:^(NSURLSessionDataTask *task, id responseObject) {
//处理返回的数据
NSDictionary * mDic=responseObject[@“result”];NSArray * arr=mDic[@"data"]; for (NSDictionary * dic in arr) { //字典转模型 Model * model=[Model mj_objectWithKeyValues:dic]; //将数据添加到数组 [self.dataArr addObject:model]; //刷新表格 在主线程刷新表格 dispatch_async(dispatch_get_main_queue(), ^{ [self.tbv reloadData]; }); }
} failure:^(NSURLSessionDataTask *task, NSError *error) {
//打印错误的信息}];
}
//实现数据源方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * string=@“string”;
YouTableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:string];
if(!cell)
{
cell=[[YouTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:string];
}
//创建model
Model * m=self.dataArr[indexPath.row];
cell.m=m;
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 200;
}
最后打开网络即可