没什么含量,做个笔记.原理是在WillDisplayXXXX
中加载下一页数据,由于是WillDisplay
,所以离显示还有一小段时间,正常情况下这段时间是差不多可以完成下一页数据的缓冲的.
#define PageItemsCount 10UL
@interface Tab4ViewController ()<ASTableDelegate, ASTableDataSource>
@end
@implementation Tab4ViewController
{
ASTableNode *_tableNode;
BOOL _appending;
NSInteger _page;
NSArray *_dataSource;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.edgesForExtendedLayout = UIRectEdgeNone;
UILabel *v1 = NewClass(UILabel);
v1.backgroundColor = GreenColor;
v1.font = [UIFont systemFontOfSize:CalcFont(40)];
v1.text = @"假装是个标题区!";
v1.frame = CGRectMake(0, 0, kScreenWidth, kSCBarHeight);
v1.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:v1];
ASTableNode *atv = [[ASTableNode alloc] initWithStyle:UITableViewStyleGrouped];
atv.delegate = self;
atv.dataSource = self;
[self.view addSubnode:atv];
_tableNode = atv;
ASTableView *tv = atv.view;
tv.showsVerticalScrollIndicator = NO;
tv.showsHorizontalScrollIndicator = NO;
[tv mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.trailing.mas_equalTo(0);
make.top.mas_equalTo(v1.mas_bottom);
make.bottom.mas_equalTo(0);
}];
_page = 1;
[APIServer getOdaysWithType:0 page:_page block:^(NSArray *respone) {
if (respone.count)
{
NSMutableArray *ds = [NSMutableArray arrayWithCapacity:respone.count];
for (NSDictionary *info in respone)
{
SampleModel *model = [SampleModel modelWithDictionary:info];
[ds addObject:model];
}
_dataSource = ds;
[self reloadData];
}
}];
}
- (void)appendPage: (NSInteger)page failedBlock: (EmptyBlock)block
{
if (_appending) return;
_appending = YES;
[APIServer getOdaysWithType:0 page:page block:^(NSArray *respone) {
if (respone.count)
{
NSMutableArray *ds = (NSMutableArray *)_dataSource;
NSMutableArray *ins = [NSMutableArray arrayWithCapacity:respone.count];
for (NSUInteger a = ds.count; a < respone.count+ds.count; ++a)
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:a inSection:0];
[ins addObject:indexPath];
}
for (NSDictionary *info in respone)
{
SampleModel *model = [SampleModel modelWithDictionary:info];
[ds addObject:model];
}
runBlockWithMain(^{
[_tableNode.view insertRowsAtIndexPaths:ins withRowAnimation:UITableViewRowAnimationNone];
});
}
else
{
if (block) block();
}
_appending = NO;
}];
}
- (void)reloadData
{
runBlockWithMain(^{
[_tableNode.view reloadData];
});
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 0.001;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 0.001;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _dataSource.count;
}
- (void)tableView:(ASTableView *)tableView willDisplayNodeForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (NO == _appending && indexPath.row >= (_dataSource.count - PageItemsCount))
{
[self appendPage:++_page failedBlock:^{
--_page;
}];
}
}
- (ASCellNode *)tableView:(ASTableView *)tableView nodeForRowAtIndexPath:(NSIndexPath *)indexPath
{
SampleModel *model = _dataSource[indexPath.row];
SampleNode *cell = [[SampleNode alloc] init];
cell.imageNode.URL = [NSURL URLWithString:model.img];
cell.titleLabel.text = model.title;
cell.introLabel.text = model.content;
cell.object = model;
return cell;
}
文/sma11case(简书作者)
原文链接:http://www.jianshu.com/p/1e09031a080c
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
原文链接:http://www.jianshu.com/p/1e09031a080c
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。