文章目录
  1. 1. 我是前言
  2. 2. 开门见山
    1. 2.1. 布局
  3. 3. 下拉刷新
  4. 4. 参考

我是前言

俗话说nozuonodie,采用了ASDK,所以踩了一✔️坑

开门见山

布局

相对布局

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//(默认每个cell都包含 : 发布日期 评论(数) 赞(数) 标题)  作者 头像 分类  cell高度设定为90
-(ASLayoutSpec*)layoutMySubViewsOfNewsCellStyleHaveImageAndAuthorWithAvatarAndCategory{
//弹簧
ASLayoutSpec *spacerSpec = [[ASLayoutSpec alloc] init];
spacerSpec.flexGrow = YES;//可伸长
spacerSpec.flexShrink = YES; //可压缩



_Avatar.flexShrink = YES;
_EditorButton.flexShrink = YES;
_releaseDateLabel.flexShrink = YES;
//创建水平布局对象
ASStackLayoutSpec *horizontalStackSpec1 = [ASStackLayoutSpec horizontalStackLayoutSpec];
horizontalStackSpec1.flexShrink = YES;
//设置中心对其
horizontalStackSpec1.alignItems = ASStackLayoutAlignItemsCenter; // center items vertically in horiz stack
//设置开始适配为开始处
horizontalStackSpec1.justifyContent = ASStackLayoutJustifyContentStart; // justify content to left
horizontalStackSpec1.flexGrow = YES;
//设置水平布局
[horizontalStackSpec1 setChildren:@[_Avatar,spacerSpec,_EditorButton,spacerSpec,_releaseDateLabel]];

_titleLabel.flexShrink = YES;
_categoryLabel.flexShrink =YES;
_commentsLabel.flexShrink =YES;

ASStackLayoutSpec *horizontalStackSpec2 = [ASStackLayoutSpec horizontalStackLayoutSpec];
horizontalStackSpec2.alignItems = ASStackLayoutAlignItemsCenter; // center items vertically in horiz stack
horizontalStackSpec2.justifyContent = ASStackLayoutJustifyContentStart; // justify content to left

horizontalStackSpec2.flexShrink = YES;
[horizontalStackSpec2 setChildren:@[_categoryLabel,spacerSpec,_commentsLabel]];

//创建垂直布局
ASStackLayoutSpec *verticalStackSpec = [ASStackLayoutSpec verticalStackLayoutSpec];

verticalStackSpec.flexShrink = YES;

[verticalStackSpec setChildren:@[horizontalStackSpec1,_titleLabel,horizontalStackSpec2]];


ASStackLayoutSpec *horizontalStackLayoutSpec = [ASStackLayoutSpec horizontalStackLayoutSpec];

horizontalStackLayoutSpec.flexShrink = YES;
horizontalStackLayoutSpec.alignItems=ASStackLayoutAlignItemsCenter;
horizontalStackLayoutSpec.justifyContent = ASStackLayoutJustifyContentStart;
[horizontalStackLayoutSpec setChildren:@[verticalStackSpec ,_rightImageView]];
//设置内边距
UIEdgeInsets insets = UIEdgeInsetsMake(10, 10,
10, 10);

//设置内边距布局
ASInsetLayoutSpec *headerInsetSpec3 = [ASInsetLayoutSpec insetLayoutSpecWithInsets:insets child:horizontalStackLayoutSpec];
headerInsetSpec3.flexShrink = YES;
headerInsetSpec3.flexGrow = YES;

return headerInsetSpec3;
}

效果图

下拉刷新

结合网络数据

先看效果图

卡顿是因为电脑开了太多程序,手机很流畅

主要代码:(自己都觉的写的很烂,没办法项目太紧)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
 
#import "NSWYViewController.h"
#import "NXTATableViewCell.h"
#import <AsyncDisplayKit/AsyncDisplayKit.h>
#import "MJRefresh.h"
#import "YYFPSLabel.h"
#import "NSWYPageNumModel.h"
#import <SVProgressHUD.h>
#import "AFNetworking.h"
#import "MJExtension.h"
#import "NSWYContent.h"
#import "NewPagedFlowView.h"
#import "PGIndexBannerSubiew.h"
#import "DetialTableViewController.h"
#import "ContentViewController.h"
#define URL @"http://123.85.2.102:8089/nswy-space/a/consultinfo/nswyConsultinfo/ws/look"
#define Width [UIScreen mainScreen].bounds.size.width
#define Height [UIScreen mainScreen].bounds.size.height

@interface NSWYViewController ()<ASTableDelegate,ASTableDataSource,NewPagedFlowViewDelegate,NewPagedFlowViewDataSource>

/** ASTableView *_tableView;*/
@property (nonatomic,strong) ASTableView * tableView;


/**imageArray*/
@property (nonatomic,strong) NSMutableArray * imageArray;
/**ScorllView视图*/
@property (nonatomic,strong) UIScrollView * Sview;

/**数据源*/
@property (nonatomic,strong) NSWYPageNumModel * homeModel;
/**NSWYContent*/
/**数据*/
@property (nonatomic,strong) NSMutableArray * contentArr;

/**评论数*/
@property (nonatomic,strong) NSMutableArray * total;

/**currentPage*/
@property (nonatomic,assign) NSInteger currentPage;

/**tableView*/
@property (nonatomic,strong) ASTableNode * tableNode;
/**count*/
@property (nonatomic,assign) NSInteger count;
/**num*/
@property (nonatomic,assign) NSInteger num;
/**<#注释#>*/
@property (nonatomic,strong) YYFPSLabel * fpsLabel;
/**注释*/
@property (nonatomic,assign) BOOL footer;
/**是否inter完成*/
@property (nonatomic,assign) BOOL isCompleteInter;

@end

@implementation NSWYViewController
-(instancetype)init{
_tableNode= [[ASTableNode alloc]initWithStyle:UITableViewStylePlain];
self = [super initWithNode:_tableNode];

if (self ) {
_tableNode.view.asyncDelegate =self;
_tableNode.view.asyncDataSource = self;
}
return self;
}

- (void)viewDidLoad {
[super viewDidLoad];
_tableNode.view.contentInset = UIEdgeInsetsMake(((Width - 84) * 9 / 16 + 24), 0, 0, 0);

for (int index = 0; index < 5; index++) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"Yosemite0%d.jpg",index]];
[self.imageArray addObject:image];
}

//滚动窗口
[self setupUI];
//第一次进入网络请求
[self setupRefresh];
//这里的MJ只是起到了指示作用,没有进行刷新操作
_tableNode.view.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
if (self.contentArr.count< self.homeModel.total) {
[_tableNode.view.mj_footer endRefreshing];
}else{
[ _tableNode.view.mj_footer endRefreshingWithNoMoreData ];
}
}];
//YYkit中抽出来的FPS标签
_fpsLabel = [[YYFPSLabel alloc]initWithFrame:CGRectMake(0, 100, 50, 50)];

[_fpsLabel sizeToFit];
// _fpsLabel.alpha = 0;
_fpsLabel.backgroundColor = [UIColor whiteColor];
[self.node.view addSubview:_fpsLabel];
[self.node.view bringSubviewToFront:_fpsLabel];

NSLog(@"_fpsLabel.text---%@",_fpsLabel.text);

}
-(void)setupRefresh{
[SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
[SVProgressHUD show];

NSMutableDictionary *params = [NSMutableDictionary dictionary];
//第一页 20条数据
params[@"pageNum"] = @1;
params[@"number"] = @20;
//初始化当前页面
self.currentPage=1;
//AFNetworking网络请求
[[AFHTTPSessionManager manager] GET:URL parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {

} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {

NSWYPageNumModel * model = [NSWYPageNumModel new];
self.num =self.contentArr.count;
//MJExtenstion
[model mj_setKeyValues:responseObject ];
self.homeModel = model;
for (int i =0 ; i<model.content.count; i++) {
NSWYContent * content = [NSWYContent new];
[content mj_setKeyValues:model.content[i]];
[self.contentArr addObject:content];

}
//就这一次用到'reloadData',ASDK刷新会导致列表回到开头
[_tableNode.view reloadData];
//记录插入状态,将数据插入最后index
_isCompleteInter =YES;
[SVProgressHUD dismiss];


} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@" ,error]] ;
}];



}
//加载更多数据
- (void)loadMoreData: (void(^)())complete withContext:(ASBatchContext*)context{

if (!_isCompleteInter) {
//判断插入状态,如果_isCompleteInter == NO,表明,前面请求的额数据为插入列表,取消插入操作,返回
[context cancelBatchFetching];
return;
}
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"pageNum"] = @(++self.currentPage) ;

params[@"number"] = @10;
[[AFHTTPSessionManager manager] GET:URL parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {

} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSWYPageNumModel * model = [NSWYPageNumModel new];
self.num = self.contentArr.count;
//内存中的数据和总数据相同,插入请求取消
if ((int)self.num == (int)model.content.count) {
[context cancelBatchFetching];

return ;
}

_isCompleteInter = NO;
[model mj_setKeyValues:responseObject ];
self.homeModel = model;
for (int i =0 ; i<model.content.count; i++) {
NSWYContent * content = [NSWYContent new];
[content mj_setKeyValues:model.content[i]];
//写入html,这和content的fcontent数据有关,主要是html形式的内容,用WKWebView加载,本文可忽略
[content.fcontent writeToFile:[self dataFilePath:content] atomically:YES encoding:NSUTF8StringEncoding error:nil];
[self.contentArr addObject:content];

NSLog(@"self.contentArr.count---%lu",(unsigned long)self.contentArr.count);

}
//回调block插入列表操作
if (complete) {
complete();
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",error]];
//失败也要设置状态
_isCompleteInter = NO;
}];

}
/**
* 数据存储路径
*
* @return 路径
*/
- (NSString *)dataFilePath:(NSWYContent*)content{
NSArray * paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * path = [paths objectAtIndex:0];


NSString * name = [NSString stringWithFormat:@"%@%@",content.ID,content.fcreatetime];

return [path stringByAppendingPathComponent: [NSString stringWithFormat:@"%@.html",name]];
}
/**
* 头部滚动视图
*/

- (void)setupUI {

//github其他人的,如有冒犯,请通知立即删除
NewPagedFlowView *pageFlowView = [[NewPagedFlowView alloc] initWithFrame:CGRectMake(0,-(( Width - 84) * 9 / 16 + 24), Width, (Width - 84) * 9 / 16 + 24)];

pageFlowView.backgroundColor = [UIColor whiteColor];
pageFlowView.delegate = self;
pageFlowView.dataSource = self;
pageFlowView.minimumPageAlpha = 0.1;
pageFlowView.minimumPageScale = 0.85;
pageFlowView.orientation = NewPagedFlowViewOrientationHorizontal;

//提前告诉有多少页
pageFlowView.orginPageCount = 5;

pageFlowView.isOpenAutoScroll = YES;

//初始化pageControl
UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, pageFlowView.frame.size.height - 24 - 8, Width, 8)];
pageFlowView.pageControl = pageControl;
[pageFlowView addSubview:pageControl];

/****************************
使用导航控制器(UINavigationController)
如果控制器中不存在UIScrollView或者继承自UIScrollView的UI控件
请使用UIScrollView作为NewPagedFlowView的容器View,才会显示正常,如下
*****************************/

UIScrollView * scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0,-(( Width - 84) * 9 / 16 + 24), Width, (( Width - 84) * 9 / 16 + 24) )];
scrollView.backgroundColor = [UIColor redColor];
[scrollView addSubview:pageFlowView];

[pageFlowView reloadData];


[_tableNode.view addSubview:pageFlowView] ;



}





#pragma mark - ASTableDelegate methods

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

// ContentViewController * detial = [ContentViewController new];
// detial.content = self.contentArr[indexPath.row];

// [self presentViewController:detial animated:YES completion:nil];

}
#pragma mark - ASTableDataSource methods

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.contentArr.count;
}

//官网说返回block保证线程安全
- (ASCellNodeBlock)tableView:(ASTableView *)tableView nodeBlockForRowAtIndexPath:(NSIndexPath *)indexPath{
ASCellNode *(^ASCellNodeBlock)() = ^ASCellNode *() {
NXTATableViewCell *cellNode = [[NXTATableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil WithNewsCellStyle:4];
cellNode.content = self.contentArr[indexPath.row];
// cellNode.titleLabel.attributedText = [[NSAttributedString alloc]initWithString:[NSString stringWithFormat:@"indexPath.row===>%ld",(long)indexPath.row]];

return cellNode;
};

return ASCellNodeBlock;

}
- (BOOL)shouldBatchFetchForTableView:(ASTableView *)tableView{
if ([tableView.mj_footer isRefreshing]) {
//当footer处于刷新状态,根据_isCompleteInter判断是否允许启用插入方法
if (_isCompleteInter) {
return YES;
}
return NO;
}
return NO;

}
//在子线程中执行
-(void)tableView:(ASTableView *)tableView willBeginBatchFetchWithContext:(ASBatchContext *)context{
[context beginBatchFetching];
[self insertNewRowsInTableNode:context];

}


- (void)insertNewRowsInTableNode:(ASBatchContext*)context
{



[self loadMoreData:^{
_isCompleteInter =NO;
NSUInteger newTotalNumberOfPhotos = self.contentArr.count;
NSLog(@"self.contentArr.count----%ld-------num --%ld",self.contentArr.count,self.num);
if ((int)self.num == (int)self.contentArr.count) {
[context cancelBatchFetching];
return ;
}

NSInteger section = 0;
NSMutableArray *indexPaths = [NSMutableArray array];

for (NSUInteger row = 0; row < newTotalNumberOfPhotos-self.num; row++) {
NSIndexPath *path = [NSIndexPath indexPathForRow:self.num+ row inSection:section];
[indexPaths addObject:path];

}
//刷新UI回到主线程,ASTableNode的view要在主线程中实现,不然崩溃
@synchronized (self) {
dispatch_async(dispatch_get_main_queue(), ^{
//插入操作
[_tableNode.view insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
_isCompleteInter = YES;
NSLog(@"%s",__FUNCTION__);
});

}

} withContext:context];
//完成插入
[context completeBatchFetching:YES];
}
@end

Demo地址

参考

文章目录
  1. 1. 我是前言
  2. 2. 开门见山
    1. 2.1. 布局
  3. 3. 下拉刷新
  4. 4. 参考