一.ViewController.m
#import "ViewController.h"
#import "ViewTableViewCell.h"
#import "AFNetworking.h"
#import "ViewMdel.h"
#import "UIImage+GIF.h"
@interface ViewController ()
{
NSArray*modelArr;
UITableView *tbv;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[selfgetData];
self.navigationItem.title = @"练习";
self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
tbv = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
tbv.delegate=self;
tbv.dataSource = self;
[self.view addSubview:tbv];
}
- (void)getData{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//拼接参数字典
NSDictionary *dic = @{
@"a":@"list",
@"c":@"data",
@"type":@10
};
[managerGET:@"http://api.budejie.com/api/api_open.php"parameters:dicsuccess:^(NSURLSessionDataTask*task,idresponseObject) {
NSLog(@"%@",responseObject);
self->modelArr = [[ViewMdel instance] saveModelWithData:[responseObject objectForKey:@"list"]];
dispatch_async(dispatch_get_main_queue(), ^{
[self->tbvreloadData];
});
}failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"错误信息%@",error);
}];
}
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
return modelArr.count;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
staticNSString*str =@"str";
ViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
if(!cell)
{
cell = [[ViewTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
}
ViewMdel*model =modelArr[indexPath.row];
[cellchangeFrameWithData:model];
//取消顶部空白
CGRectframe=CGRectMake(0,0,0,CGFLOAT_MIN);
tableView.tableHeaderView=[[UIView alloc]initWithFrame:frame];
//取消阴影
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.layer.borderWidth = 10;
cell.layer.borderColor = [UIColor colorWithRed:215.0/255.0 green:215.0/255.0 blue:215.0/255.0 alpha:1.0].CGColor;
cell.imgTou.backgroundColor = [UIColor cyanColor];
cell.mainTitle.backgroundColor = [UIColor magentaColor];
cell.subTitle.backgroundColor = [UIColor greenColor];
cell.contentTitle.backgroundColor = [UIColor redColor];
cell.BtnTitle.backgroundColor = [UIColor brownColor];
cell.lineView.backgroundColor = [UIColor colorWithRed:237/255.0 green:237/255.0 blue:237/255.0 alpha:1.0];
cell.imgBtnOne.backgroundColor = [UIColor purpleColor];
cell.l1.backgroundColor = [UIColor greenColor];
cell.imgBtnTwo.backgroundColor = [UIColor blueColor];
cell.l2.backgroundColor = [UIColor greenColor];
cell.imgBtnThree.backgroundColor = [UIColor orangeColor];
cell.l3.backgroundColor = [UIColor greenColor];
cell.imgBtnFour.backgroundColor = [UIColor cyanColor];
cell.l4.backgroundColor = [UIColor greenColor];
cell.mainTitle.text= model.name;
cell.subTitle.text= model.create_time;
[cell.BtnTitle setTitle:@"..." forState:UIControlStateNormal];
[cell.BtnTitle setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
cell.BtnTitle.titleLabel.font = [UIFont systemFontOfSize:40];
cell.contentTitle.text= model.text;
cell.l1.text= model.ding;
cell.l2.text= model.hate;
cell.l3.text= model.repost;
cell.l4.text= model.favourite;
return cell;
}
二.ViewTableViewCell.h
#import
#import "ViewMdel.h"
@interfaceViewTableViewCell :UITableViewCell
@property(nonatomic,strong)UIImageView *imgTou;
@property(nonatomic,strong)UILabel *mainTitle;
@property(nonatomic,strong)UILabel *subTitle;
@property(nonatomic,strong)UIButton *BtnTitle;
@property(nonatomic,strong)UILabel *contentTitle;
@property(nonatomic,strong)UIView *lineView;
@property(nonatomic,strong)UIImageView *contentImg;
@property(nonatomic,strong)UIButton *imgBtnOne;
@property(nonatomic,strong)UILabel *l1;
@property(nonatomic,strong)UIButton *imgBtnTwo;
@property(nonatomic,strong)UILabel *l2;
@property(nonatomic,strong)UIButton *imgBtnThree;
@property(nonatomic,strong)UILabel *l3;
@property(nonatomic,strong)UIButton *imgBtnFour;
@property(nonatomic,strong)UILabel *l4;
- (void)changeFrameWithData:(ViewMdel*)model;
三.ViewTableViewCell.m
#import "ViewTableViewCell.h"
#import "SDAutoLayout.h"
#import "AFNetworking.h"
#import "UIImage+GIF.h"
#import "UIImageView+WebCache.h"
@implementationViewTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier{
self= [superinitWithStyle:stylereuseIdentifier:reuseIdentifier];
if(self){
[selfimgTou];
[selfmainTitle];
[selfsubTitle];
[selfBtnTitle];
[self contentTitle];
[selfcontentImg];
[selflineView];
[selfimgBtnOne];
[selfl1];
[selfimgBtnTwo];
[selfl2];
[selfimgBtnThree];
[selfl3];
[selfimgBtnFour];
[selfl4];
}
return self;
}
//头像
-(UIImageView *)imgTou
{
if(!_imgTou)
{
_imgTou = [[UIImageView alloc]init];
[self.contentView addSubview:_imgTou];
_imgTou.sd_layout
.topSpaceToView(self.contentView, 20)
.leftSpaceToView(self.contentView, 20)
.widthIs(60)
.heightIs(60);
}
return _imgTou;
}
//主标题
-(UILabel*)mainTitle
{
if (!_mainTitle)
{
_mainTitle= [[UILabelalloc]init];
[self.contentView addSubview:_mainTitle];
_mainTitle.sd_layout
.topSpaceToView(self.contentView, 20)
.leftSpaceToView(self.contentView, 100)
.widthIs(150)
.heightIs(20);
}
return _mainTitle;
}
//副标题
-(UILabel*)subTitle
{
if (!_subTitle)
{
_subTitle= [[UILabelalloc]init];
[self.contentView addSubview:_subTitle];
_subTitle.sd_layout
.topSpaceToView(self.contentView, 50)
.leftSpaceToView(self.contentView, 100)
.widthIs(250)
.heightIs(20);
}
return _subTitle;
}
// ...标题
-(UIButton*)BtnTitle
{
if (!_BtnTitle)
{
_BtnTitle = [[UIButton alloc]init];
[self.contentView addSubview:_BtnTitle];
_BtnTitle.sd_layout
.topSpaceToView(self.contentView, 8)
.leftSpaceToView(self.contentView, 350)
.widthIs(50)
.heightIs(20);
}
return _BtnTitle;
}
//内容标题
-(UILabel*)contentTitle
{
if (!_contentTitle)
{
_contentTitle= [[UILabelalloc]init];
[self.contentView addSubview:_contentTitle];
_contentTitle.sd_layout
.topSpaceToView(self.contentView, 100)
.leftSpaceToView(self.contentView, 20)
.widthIs(400)
.heightIs(20);
}
return _contentTitle;
}
//内容图片
-(UIImageView*)contentImg
{
if (!_contentImg)
{
_contentImg = [[UIImageView alloc]init];
[self.contentView addSubview:_contentImg];
_contentImg.sd_layout
.topSpaceToView(self.contentView, 130)
.leftSpaceToView(self.contentView, 20)
.widthIs(self.frame.size.width+50)
.heightIs(300);
}
return _contentImg;
}
//分割线
-(UIView*)lineView
{
if (!_lineView)
{
_lineView= [[UIViewalloc]init];
[self.contentView addSubview:_lineView];
_lineView.sd_layout
.topSpaceToView(self.contentView, 440)
.widthIs(self.frame.size.width+90)
.heightIs(2);
}
return _lineView;
}
//按钮1
-(UIButton*)imgBtnOne
{
if (!_imgBtnOne)
{
_imgBtnOne = [[UIButton alloc]init];
[self.contentView addSubview:_imgBtnOne];
_imgBtnOne.sd_layout
.topSpaceToView(self.contentView, 460)
.leftSpaceToView(self.contentView,45)
.widthIs(40)
.heightIs(40);
}
return _imgBtnOne;
}
-(UILabel *)l1
{
if(!_l1)
{
_l1= [[UILabelalloc]init];
[self.contentView addSubview:_l1];
_l1.sd_layout
.topSpaceToView(self.contentView, 465)
.leftSpaceToView(self.contentView,85)
.widthIs(25)
.heightIs(30);
}
return _l1;
}
//按钮2
-(UIButton*)imgBtnTwo
{
if (!_imgBtnTwo)
{
_imgBtnTwo = [[UIButton alloc]init];
[self.contentView addSubview:_imgBtnTwo];
_imgBtnTwo.sd_layout
.topSpaceToView(self.contentView, 460)
.leftSpaceToView(self.contentView,125)
.widthIs(40)
.heightIs(40);
}
return _imgBtnTwo;
}
-(UILabel *)l2
{
if(!_l2)
{
_l2= [[UILabelalloc]init];
[self.contentView addSubview:_l2];
_l2.sd_layout
.topSpaceToView(self.contentView, 465)
.leftSpaceToView(self.contentView,165)
.widthIs(25)
.heightIs(30);
}
return _l2;
}
//按钮3
-(UIButton*)imgBtnThree
{
if (!_imgBtnThree)
{
_imgBtnThree = [[UIButton alloc]init];
[self.contentView addSubview:_imgBtnThree];
_imgBtnThree.sd_layout
.topSpaceToView(self.contentView, 460)
.leftSpaceToView(self.contentView,205)
.widthIs(40)
.heightIs(40);
}
return _imgBtnThree;
}
-(UILabel *)l3
{
if(!_l3)
{
_l3= [[UILabelalloc]init];
[self.contentView addSubview:_l3];
_l3.sd_layout
.topSpaceToView(self.contentView, 465)
.leftSpaceToView(self.contentView,245)
.widthIs(25)
.heightIs(30);
}
return _l3;
}
//按钮4
-(UIButton*)imgBtnFour
{
if (!_imgBtnFour)
{
_imgBtnFour = [[UIButton alloc]init];
[self.contentView addSubview:_imgBtnFour];
_imgBtnFour.sd_layout
.topSpaceToView(self.contentView, 460)
.leftSpaceToView(self.contentView,285)
.widthIs(40)
.heightIs(40);
}
return _imgBtnFour;
}
//-(UILabel *)l4
//{
// if (!_l4)
// {
// _l4 = [[UILabel alloc]init];
// [self.contentView addSubview:_l4];
//
// _l1.sd_layout
// .topSpaceToView(self.contentView, 465)
// .leftSpaceToView(self.contentView,325)
// .widthIs(25)
// .heightIs(30);
//
// }
// return _l4;
//}
- (void)changeFrameWithData:(ViewMdel*)model{
[self.imgTou sd_setImageWithURL:[NSURL URLWithString:model.cdn_img]];
if(model.is_gif==0){
[self.contentImg sd_setImageWithURL:[NSURL URLWithString:model.image0]];
}else{
NSString*urlStr = model.image0;
NSURL*url = [NSURLURLWithString:urlStr];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage*img = [UIImagesd_animatedGIFWithData:data];
self.contentImg.image= img;
}
}
四.ViewMdel.h
#import
#import
#import "SDImageCache.h"
@interfaceViewMdel :NSObject
@property(nonatomic,strong)NSString*cdn_img; ///<用户头像
@property(nonatomic,strong)NSString*name; ///<用户名称
@property(nonatomic,strong)NSString*create_time; ///<创建时间
@property(nonatomic,strong)NSString*text; ///<内容
@property(nonatomic,strong)NSString*image0; ///<图片
@property(nonatomic,strong)NSString*ding; ///<点赞数量
@property(nonatomic,strong)NSString*hate; ///<不喜欢
@property(nonatomic,strong)NSString*repost; ///<分享
@property(nonatomic,strong)NSString*favourite; ///<评论
@property(nonatomic,assign)CGFloatwidth; ///<宽度
@property(nonatomic,assign)CGFloatheight; ///<高度
@property(nonatomic,assign)intis_gif; ///<是否是动态图片
+(instancetype)instance;
- (NSArray*)saveModelWithData:(NSArray*)data;
五.ViewMdel.m
#import "ViewMdel.h"
@implementation ViewMdel
+(instancetype)instance{
ViewMdel*model = [[ViewMdelalloc]init];
returnmodel;
}
- (NSArray*)saveModelWithData:(NSArray*)data{
NSMutableArray *modelArr = [NSMutableArray array];
for(NSDictionary*dicindata) {
ViewMdel*model = [[ViewMdelalloc]init];
[modelsetValuesForKeysWithDictionary:dic];
[modelArraddObject:model];
}
return[modelArrcopy];
}
- (void)setValue:(id)value forUndefinedKey:(NSString*)key{
}