獲取網(wǎng)絡(luò)數(shù)據(jù)

一.ViewController.m

#import "ViewController.h"

#import "ViewTableViewCell.h"

#import "AFNetworking.h"

#import "ViewMdel.h"

#import "UIImage+GIF.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

{

? ? NSArray*modelArr;

? ? UITableView *tbv;

}

@end

@implementation ViewController

- (void)viewDidLoad

{

? ? [super viewDidLoad];


? ? [selfgetData];


? ? self.navigationItem.title = @"練習(xí)";

? ? 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];

? ? //拼接參數(shù)字典

? ? 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(@"錯(cuò)誤信息%@",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;

}

//主標(biāo)題

-(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;

}

//副標(biāo)題

-(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;

}

// ...標(biāo)題

-(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;

}

//內(nèi)容標(biāo)題

-(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;

}

//內(nèi)容圖片

-(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;? ? ? ? ? ? ? ///<用戶(hù)頭像

@property(nonatomic,strong)NSString*name;? ? ? ? ? ? ? ? ///<用戶(hù)名稱(chēng)

@property(nonatomic,strong)NSString*create_time;? ? ? ? ? ///<創(chuàng)建時(shí)間

@property(nonatomic,strong)NSString*text;? ? ? ? ? ? ? ? ///<內(nèi)容

@property(nonatomic,strong)NSString*image0;? ? ? ? ? ? ? ///<圖片

@property(nonatomic,strong)NSString*ding;? ? ? ? ? ? ? ? ///<點(diǎn)贊數(shù)量

@property(nonatomic,strong)NSString*hate;? ? ? ? ? ? ? ? ///<不喜歡

@property(nonatomic,strong)NSString*repost;? ? ? ? ? ? ? ///<分享

@property(nonatomic,strong)NSString*favourite;? ? ? ? ? ? ///<評(píng)論

@property(nonatomic,assign)CGFloatwidth;? ? ? ? ? ? ? ? ? ///<寬度

@property(nonatomic,assign)CGFloatheight;? ? ? ? ? ? ? ? ///<高度

@property(nonatomic,assign)intis_gif;? ? ? ? ? ? ? ? ? ? ///<是否是動(dòng)態(tài)圖片

+(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{


}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容