解析
Model.h
@property(nonatomic,strong)NSString *pic;
@property(nonatomic,strong)NSString *title;
@property(nonatomic,strong)NSString *url;
LoadData.h
#import <Foundation/Foundation.h>
typedef void(^PassDataBlock)(NSMutableArray *arr);
@interface FirstLoadData : NSObject
+ (instancetype)shareLoadData;
- (void)getData:(PassDataBlock )block;
@end
.m
#import "FirstModel.h"
#import <YYModel.h>
static FirstLoadData *ld;
@implementation FirstLoadData
+ (instancetype)shareLoadData
{
if (!ld) {
ld = [[FirstLoadData alloc]init];
}
return ld;
}
- (void)getData:(PassDataBlock )block
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://api.jisuapi.com/news/get"]];
request.HTTPMethod = @"POST";
NSString *str = [NSString stringWithFormat:@"channel=%@&start =%d&num=%d&appkey=%@",@"頭條",0,20,@"c3b84a436b060866"];
NSData *bodyData = [str dataUsingEncoding:NSUTF8StringEncoding];
request.HTTPBody = bodyData;
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSDictionary *dict = [dic objectForKey:@"result"];
NSArray *arr = [dict objectForKey:@"list"];
NSMutableArray *mArr = [NSMutableArray array];
for (NSDictionary *dictionary in arr) {
// FirstModel *m = [[FirstModel alloc]init];
// m.title = dictionary[@"title"];
// m.pic = dictionary[@"pic"];
// m.url = dictionary[@"url"];
FirstModel *yaowen = [FirstModel yy_modelWithDictionary:dictionary];
[mArr addObject:yaowen];
NSLog(@"%@",mArr);
}
dispatch_async(dispatch_get_main_queue(), ^{
block(mArr);
});
}];
[task resume];
}
@end
UIViewController.m
#import "FirstViewController.h"
#import "ImageScrollView.h"
#import "TableViewCell.h"
#import "FirstLoadData.h"
#import "FirstModel.h"
#import "UIImageView+WebCache.h"
#import "NextViewController.h"
@interface FirstViewController ()<ImageScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>
{
NSMutableArray *marr;
MJRefreshNormalHeader *_header;
}
@property(nonatomic,strong)UITableView *tableView;;
@end
@implementation FirstViewController
-(UITableView *)tableView{
if (!_tableView) {
_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, FIT_Y(260), self.view.frame.size.width,SCREEN_H-FIT_Y(230)) style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.rowHeight = 290;
_tableView.backgroundColor = [UIColor clearColor];
}
return _tableView;
}
- (void)ShuaXin
{
// 設定回撥(一旦進入重新整理狀態,就呼叫target的action,也就是呼叫self的loadNewData方法)
_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
// 設定自動切換透明度(在導航欄下面自動隱藏)
_header.automaticallyChangeAlpha = YES;
// 隱藏時間
_header.lastUpdatedTimeLabel.hidden = YES;
// 設定header
self.tableView.mj_header = _header;
}
- (void)loadNewData
{
[self viewWillAppear:YES];
[self.tableView reloadData];
[_header endRefreshing];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.tableView];
[self ShuaXin];
[[FirstLoadData shareLoadData]getData:^(NSMutableArray *arr) {
marr = arr;
[_tableView reloadData];
}];
}
#pragma mark ---UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return marr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *iden = @"cell";
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
if (cell == nil) {
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:iden];
}
FirstModel *m = marr[indexPath.row];
cell.title.text = m.title;
[cell.image sd_setImageWithURL:[NSURL URLWithString:m.pic] placeholderImage:[UIImage imageNamed:@"103.jpg"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
}];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
FirstModel *m = marr[indexPath.row];
NextViewController *dvc = [[NextViewController alloc]init];
dvc.weburl = m.url;
[self.navigationController pushViewController:dvc animated:YES];
}
@end
相關文章
- 解析-解析器
- ORACLE SQL解析之硬解析和軟解析OracleSQL
- 解析-HTML 解析器HTML
- Nginx(六):配置解析之location解析Nginx
- XML 檔案解析實踐 (DOM 解析)XML
- dom解析和sax解析的區別
- 雲解析DNS如何實現智慧解析?DNS
- 什麼是智慧DNS雲解析?雲解析如何實現智慧解析效果?DNS
- 雲解析VS傳統解析,雲解析到底有哪些實際用途?
- quicklink解析UI
- ThreadLocal解析thread
- 解析 SQLiteOpenHelperSQLite
- LongAdder解析
- Handler解析
- xpath解析
- this 全面解析
- 容器解析
- 解析ThreadLocalthread
- OkHttp解析HTTP
- FileReader 解析
- this全面解析
- AQS解析AQS
- ThreadLocal 解析thread
- Go解析Go
- Semaphore解析
- Xml解析XML
- PointRend解析
- Handler訊息機制完全解析Handler解析
- 深入解析 Spring AI 系列:解析函式呼叫SpringAI函式
- 什麼是DNS雲解析?雲解析和普通解析有什麼區別?DNS
- 軟解析、硬解析的一個小測試
- 什麼是DNS解析?如何提升DNS解析安全?DNS
- 深入解析 Spring AI 系列:解析OpenAI介面對接SpringOpenAI
- this 全面解析(一)
- DNS解析流程DNS
- ConstraintLayout 全解析AI
- this 繫結解析
- Immer 全解析
- this全面解析(二)