UITableView无缝加载分页数据

没什么含量,做个笔记.原理是在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
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值