#import
#import "Model.h"
NS_ASSUME_NONNULL_BEGIN
@interfaceZDYTableViewCell :UITableViewCell
@property (nonatomic,strong)UILabel *titleLab;
@property (nonatomic,strong)UIImageView *img;
- (void)loadData:(Model*)mod;
@end
NS_ASSUME_NONNULL_END
#import "ZDYTableViewCell.h"
@implementationZDYTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier{
if(self=[superinitWithStyle:stylereuseIdentifier:reuseIdentifier]) {
self.img=[[UIImageView alloc]initWithFrame:CGRectMake(10, 0, 80, 80)];
self.titleLab=[[UILabel alloc]initWithFrame:CGRectMake(100, 0, 300, 50)];
self.titleLab.numberOfLines=0;
[self.contentViewaddSubview:self.img];
[self.contentViewaddSubview:self.titleLab];
}
return self;
}
- (void)loadData:(Model*)mod{
if(mod) {
self.titleLab.text=mod.title;
self.img.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:mod.imgsrc]]];
}
}
@end
//
// ViewController.m
// AFNet&coreData
//
// Created by 刘敏 on 2018/12/15.
// Copyright © 2018年 刘敏. All rights reserved.
//
#import "ViewController.h"
#import "AFNetworking.h"
#import "Model.h"
#import "ZDYTableViewCell.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *data;
@end
staticNSString*oj =@"cell";
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.data=[[NSMutableArray alloc]init];
[self CreateAFNet];
[self.viewaddSubview:self.table];
}
- (UITableView*)table{
if (!_tableView) {
_tableView=[[UITableView alloc]initWithFrame:self.view.frame];
_tableView.delegate=self;
_tableView.dataSource=self;
}
[_tableView registerClass:[ZDYTableViewCell class] forCellReuseIdentifier:oj];
return _tableView;
}
- (NSInteger)tableView:(nonnullUITableView*)tableView numberOfRowsInSection:(NSInteger)section {
return self.data.count;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
ZDYTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:oj];
// cell.selectionStyle = UITableViewCellSelectionStyleNone;
if(self.data.count>0) {
Model*mod=self.data[indexPath.row];
[cellloadData:mod];
}
returncell;
}
- (void)CreateAFNet{
AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
[managerGET:@"http://c.m.163.com/nc/article/headline/T1348647853363/0-20.html" parameters:nil headers:nil progress:^(NSProgress * _Nonnull downloadProgress) {
}success:^(NSURLSessionDataTask*_Nonnulltask,id _NullableresponseObject) {
NSLog(@"%@",responseObject);
for( NSDictionary *dicinresponseObject[@"T1348647853363"]) {
Model *mod=[[Model alloc]init];
[mod setValuesForKeysWithDictionary:dic];
[self.data addObject:mod];
[self.table reloadData];
}
} failure:^(NSURLSessionDataTask *_Nullabletask, NSError *_Nonnullerror) {
NSLog(@"%@",error);
}];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 80;
}
@end
网友评论