SDWebImage

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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市噪舀,隨后出現(xiàn)的幾起案子夺巩,更是在濱河造成了極大的恐慌惭蟋,老刑警劉巖贺氓,帶你破解...
    沈念sama閱讀 222,252評(píng)論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異戚宦,居然都是意外死亡艺配,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,886評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門(mén)伙菜,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)轩缤,“玉大人,你說(shuō)我怎么就攤上這事贩绕』鸬模” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 168,814評(píng)論 0 361
  • 文/不壞的土叔 我叫張陵淑倾,是天一觀的道長(zhǎng)馏鹤。 經(jīng)常有香客問(wèn)我,道長(zhǎng)娇哆,這世上最難降的妖魔是什么湃累? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,869評(píng)論 1 299
  • 正文 為了忘掉前任勃救,我火速辦了婚禮,結(jié)果婚禮上脱茉,老公的妹妹穿的比我還像新娘剪芥。我一直安慰自己,他們只是感情好琴许,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,888評(píng)論 6 398
  • 文/花漫 我一把揭開(kāi)白布税肪。 她就那樣靜靜地躺著,像睡著了一般榜田。 火紅的嫁衣襯著肌膚如雪益兄。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 52,475評(píng)論 1 312
  • 那天箭券,我揣著相機(jī)與錄音净捅,去河邊找鬼。 笑死辩块,一個(gè)胖子當(dāng)著我的面吹牛蛔六,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播废亭,決...
    沈念sama閱讀 41,010評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼国章,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了豆村?” 一聲冷哼從身側(cè)響起液兽,我...
    開(kāi)封第一講書(shū)人閱讀 39,924評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎掌动,沒(méi)想到半個(gè)月后四啰,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,469評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡粗恢,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,552評(píng)論 3 342
  • 正文 我和宋清朗相戀三年柑晒,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片眷射。...
    茶點(diǎn)故事閱讀 40,680評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡敦迄,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出凭迹,到底是詐尸還是另有隱情,我是刑警寧澤苦囱,帶...
    沈念sama閱讀 36,362評(píng)論 5 351
  • 正文 年R本政府宣布嗅绸,位于F島的核電站,受9級(jí)特大地震影響撕彤,放射性物質(zhì)發(fā)生泄漏鱼鸠。R本人自食惡果不足惜猛拴,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,037評(píng)論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望蚀狰。 院中可真熱鬧愉昆,春花似錦、人聲如沸麻蹋。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,519評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)扮授。三九已至芳室,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間刹勃,已是汗流浹背堪侯。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,621評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留荔仁,地道東北人伍宦。 一個(gè)月前我還...
    沈念sama閱讀 49,099評(píng)論 3 378
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像乏梁,于是被迫代替她去往敵國(guó)和親次洼。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,691評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容