業(yè)務(wù)處理層:
LoadData.h: ? ?#import@interface LoadData : NSObject
// 創(chuàng)建單利方法
+ (instancetype)shareLoadData;
// 解析方法
- (void)getContent:(NSString *)url;
@end
LoadData.m: ? ? ?#import "LoadData.h"
// 創(chuàng)建單利變量
static LoadData *ld;
@implementation LoadData
{
? ? __block NSMutableDictionary *ContentDic,*ContentDic1;
}
// 創(chuàng)建單利方法
+ (instancetype)shareLoadData{
? ? //
? ? static dispatch_once_t onceToken;
? ? dispatch_once(&onceToken, ^{
? ? ? ? ld = [[LoadData alloc]init];
? ? });
? ? return ld;
}
+ (instancetype)allocWithZone:(struct _NSZone *)zone{
? ? if (!ld) {
? ? ? ? ld = [super allocWithZone:zone];
? ? }
? ? return ld;
}
- (id)copy{
? ? return self;
}
- (id)mutableCopy{
? ? return self;
}
// 解析方法
- (void)getContent:(NSString *)url{
? ? NSURLSession *session = [NSURLSession sharedSession];
? ? NSURLSessionTask *task = [session dataTaskWithURL:[NSURL URLWithString:url] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
? ? ? ? // 用系統(tǒng)自帶的JSON解析
? ? ? ? self->ContentDic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
? ? ? ? // 回到主線程發(fā)送通知
? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{
? ? ? ? ? ? [[NSNotificationCenter defaultCenter]postNotificationName:@"getContent" object:self->ContentDic];
? ? ? ? });
? ? }];
? ? [task resume];
}
@end
MyTableViewCell.h: ??#import@interface MyTableViewCell : UITableViewCell
@property(nonatomic,strong)UILabel *titleLb,*timeLb;
@property(nonatomic,strong)UIImageView *img;
@end
MyTableViewCell.m: ? ?#import "MyTableViewCell.h"
@implementation MyTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
? ? self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
? ? if (self) {
? ? ? ? [self addSubview:self.titleLb];
? ? ? ? [self addSubview:self.timeLb];
? ? ? ? [self addSubview:self.img];
? ? }
? ? return self;
}
- (UILabel *)titleLb{
? ? if (!_titleLb) {
? ? ? ? // 初始化位置
? ? ? ? _titleLb = [[UILabel alloc]initWithFrame:CGRectMake(10, 20,240, 100)];
? ? ? ? // 設(shè)置自動(dòng)換行
? ? ? ? _titleLb.numberOfLines = 0;
? ? ? ? // 設(shè)置背景顏色
? ? ? ? // _titleLb.backgroundColor = [UIColor greenColor];
? ? ? ? // 設(shè)置字體大小
? ? ? ? _titleLb.font = [UIFont systemFontOfSize:22];
? ? }
? ? return _titleLb;
}
- (UIImageView *)img{
? ? if (!_img) {
? ? ? ? // 初始化位置
? ? ? ? _img = [[UIImageView alloc]initWithFrame:CGRectMake(260,10,120,120)];
? ? ? ? // 設(shè)置背景顏色
? ? ? ? _img.backgroundColor = [UIColor yellowColor];
? ? }
? ? return _img;
}
- (UILabel *)timeLb{
? ? if (!_timeLb) {
? ? ? ? // 初始化位置
? ? ? ? _timeLb = [[UILabel alloc]initWithFrame:CGRectMake(240,130,195,40)];
? ? ? ? // 設(shè)置背景顏色
? ? ? ? //? ? ? ? _timeLb.backgroundColor = [UIColor blueColor];
? ? ? ? // 設(shè)置字體大小
? ? ? ? _timeLb.font = [UIFont systemFontOfSize:16];
? ? }
? ? return _timeLb;
}
- (void)awakeFromNib {
? ? [super awakeFromNib];
? ? // Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
? ? [super setSelected:selected animated:animated];
? ? // Configure the view for the selected state
}
@end
viewController.m: ??#import "ViewController.h"
#import "UIImageView+WebCache.h"
#import "LoadData.h"
#import "MyTableViewCell.h"
#import "MJRefresh.h"
#import "MJRefreshHeader.h"
#import "MJRefreshAutoFooter.h"
#import "MBProgressHUD.h"
@interface ViewController (){
? ? // 創(chuàng)建全局表格
? ? UITableView *_tableView;
? ? // 創(chuàng)建字典用于接收
? ? NSDictionary *_ContentDic;
? ? // 創(chuàng)建數(shù)組用于獲取字典中的數(shù)組
? ? NSArray *_ContentArray;
? ? // 定義網(wǎng)址
? ? NSURL *_PathUrl;
? ? // 定義變量
? ? int *num;
}
@end
@implementation ViewController
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? num = 3;
? ? // 注冊(cè)通知
? ? [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getContent:) name:@"getContent" object:nil];
? ? // 初始化表格
? ? _tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
? ? // 設(shè)置代理
? ? _tableView.delegate = self;
? ? _tableView.dataSource = self;
? ? // 添加到主視圖
? ? [self.view addSubview:_tableView];
? ? // 調(diào)用業(yè)務(wù)處理層方法
? ? [self Load];
? ? // 創(chuàng)建下拉刷新控件
? ? _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(MJHeader)];
? ? _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
}
#pragma mark - 創(chuàng)建業(yè)務(wù)處理層方法
- (void)Load{
? ? // 每次刷新增加新聞數(shù)量
? ? num = num + 1;
? ? // 創(chuàng)建業(yè)務(wù)處理層對(duì)象
? ? LoadData *ld = [LoadData shareLoadData];
? ? NSString *TEXT_URL = [NSString stringWithFormat:@"https://way.jd.com/jisuapi/get?channel=頭條&num=%d&start=0&appkey=54e619938bc38b40151c7bc35a29067e",num];
? ? // 將網(wǎng)址轉(zhuǎn)換成UTF8格式傳過(guò)去
? ? NSString *str = [TEXT_URL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
? ? // 調(diào)用里面的方法
? ? [ld getContent:str];
}
#pragma mark - 發(fā)送通知接收方法
- (void)getContent:(NSNotification *)notifi{
? ? // 接收數(shù)據(jù)
? ? _ContentDic = notifi.object;
? ? // dic = [_ContentDic mutableCopy];
? ? // 刷新表格
? ? [_tableView reloadData];
}
#pragma mark - 下拉刷新控件方法
- (void)MJHeader{
? ? // 創(chuàng)建菊花加載控件
? ? MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
? ? // 回到主線程刷新表格
? ? dispatch_async(dispatch_get_main_queue(), ^{
? ? ? ? // 刷新表格
? ? ? ? [self->_tableView reloadData];
? ? ? ? // 刷新時(shí)間
? ? ? ? [HUD setMinShowTime:1];
? ? ? ? // 一秒后隱藏
? ? ? ? [HUD hide:YES];
? ? });
? ? // 刷新
? ? [_tableView.mj_header endRefreshing];
}
#pragma mark - 上拉刷新控件方法
- (void)loadMore{
? ? // 調(diào)用業(yè)務(wù)處理層方法
? ? [self Load];
? ? // 刷新表格
? ? [_tableView reloadData];
? ? // 結(jié)束上拉刷新
? ? [_tableView.mj_footer endRefreshing];
}
#pragma mark - 表格數(shù)據(jù)源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
? ? // 用數(shù)組接收字典中的數(shù)據(jù)
? ? _ContentArray = _ContentDic[@"result"][@"result"][@"list"];
? ? // 返回?cái)?shù)組中的個(gè)數(shù)
? ? return _ContentArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
? ? static NSString *str = @"cell";
? ? MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
? ? // 判斷
? ? if (!cell) {
? ? ? ? // 創(chuàng)建cell
? ? ? ? cell = [[MyTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
? ? }
? ? // 獲取數(shù)組中的字典
? ? NSDictionary *dic = _ContentArray[indexPath.row];
? ? // 獲取標(biāo)題并賦值
? ? cell.titleLb.text = dic[@"title"];
? ? // 獲取時(shí)間并賦值
? ? cell.timeLb.text = dic[@"time"];
? ? // 用SDWebImage第三方庫(kù)加載圖片
? ? NSString *URlStr = dic[@"pic"];
? ? _PathUrl = [NSURL URLWithString:URlStr];
? ? [cell.img sd_setImageWithURL:_PathUrl placeholderImage:[UIImage imageNamed:@"機(jī)器人.jpg"]];
? ? return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
? ? return 180;
}
@end