<pre name="code" class="objc">#import <UIKit/UIKit.h>
#import "TableModel.h"
@interface MyTableViewCell : UITableViewCell
@property(nonatomic,strong)UIImageView * picture;//商品图片
//从模型加载数据(配置,布局;显示配置信息命令)
- (void)config:(TableModel*)model;
@end
#import "MyTableViewCell.h"
#import "UIKit+AFNetworking.h"
#define URL @"http://jiudu.jbserver.me/mobile/%@"
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width//屏幕的宽
@implementation MyTableViewCell
//这个方法连是对我们看到的东西进行搭建界面(这个方法叫重写)
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
//这个写法简洁易懂
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
//初始化视图对象
//图片
self.picture = [[UIImageView alloc]initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, 140)];
//添加到父视图
[self.contentView addSubview:self.picture];
}
return self;
}
//由于setImageWithURL ,所以此方法必须写在MVC的V里面.终于理解了个原理.(config这个单词的意思是:配置,布局;显示配置信息命令)所以用这个单词可以非常明确表达意思.逼格马上起来了.
- (void)config:(TableModel *)model{
//拼接字符串
NSString * str = [NSString stringWithFormat:URL,model.picture];
NSLog(@"%@",str);
[self.picture setImageWithURL:[NSURL URLWithString:str]];
}
#import <Foundation/Foundation.h>
@interface TableModel : NSObject
@property (nonatomic,copy) NSString * picture;//图片
@end
#import "TableModel.h"
@implementation TableModel
//重写防止崩溃方法
//赋值
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
if ([key isEqualToString:@"content"]) {
//就是任性,不喜欢接口里的key
self.picture = value;
}
}
//取值
- (instancetype)valueForUndefinedKey:(NSString *)key{
return nil;
}
@end
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width//屏幕的宽
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height//屏幕的高
#import "ViewController.h"
#import "MyTableViewCell.h"//列表cell
#import "TableModel.h"//列表model
#import <AFNetworking.h>
//#import <UIImageView+WebCache.h>
//#import "AFNetworking.h"
//#import "UIImageView+WebCache.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView * tableView;//列表
@property (nonatomic,strong)NSMutableArray * dataArray;//列表的数据源
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"模板";
//创建列表
[self createTableView];
//网络请求加载数据
[self loadData];
}
//创建列表
- (void)createTableView{
//关闭自动布局
self.automaticallyAdjustsScrollViewInsets = NO;
self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
[self.view addSubview:self.tableView];
//设置代理
self.tableView.delegate = self;
self.tableView.dataSource = self;
//注册tableView
[_tableView registerClass:[MyTableViewCell class] forCellReuseIdentifier:@"cellID"];
}
//网络请求加载数据
- (void)loadData{
//初始化数组
self.dataArray = [NSMutableArray array];
AFHTTPSessionManager * session=[[AFHTTPSessionManager alloc]init];
session.requestSerializer=[AFHTTPRequestSerializer serializer];
session.responseSerializer=[AFHTTPResponseSerializer serializer];
[session GET:@"http://jiudu.jbserver.me/apicore/index.php/index/get_banner" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//最外层
id json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
//由外至内第二层,arr里面内容就是数组里的内容
NSArray * arr = [json objectForKey:@"data"];
//我自己的理解:网络请求数据,通过循环把数据存到模型里面.图片的特殊情况,模型存在的数据只是后面的,需要拼接前面的.拼接后,打印出的字符转试一试能不能再浏览器里面打开看到图片.如果能看到图片就setImageWithURL获取.
for (NSDictionary * newDict in arr) {
TableModel * model = [[TableModel alloc]init];
[model setValuesForKeysWithDictionary:newDict];
[self.dataArray addObject:model];
}
//刷新TableView
[self.tableView reloadData];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error.description);
}];
}
#pragma mark - tableView代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.dataArray.count;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MyTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cellID" forIndexPath:indexPath];
TableModel * model = self.dataArray[indexPath.row];
[cell config:model];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 200;
}
转载请注明原文地址: https://ju.6miu.com/read-1125346.html