ios基础篇(二十八)—— UITableView的上拉加载

本文介绍了一个简单的UITableView上拉加载更多数据的实现方法。通过在TableView的底部添加加载指示器并在用户上拉时触发数据加载,可以实现流畅的用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文主要展示一个demo实现UITableView的上拉加载数据;

先看看效果图

接着上拉,加载更多数据

主要实现的效果是在我们上拉结束拖拽之后,开始加载数据,数据加载的过程中有滚动轮提示用户正在加载,加载完成后滚动轮消失,加载的数据出现。直接看代码吧:

demo:

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>{

    UITableView *tableView;
    
    //数据数组
    NSMutableArray *dataArray;
    //上拉时的加载数据
    NSMutableArray *moreArray;
    //数据数量
    NSUInteger dataNumber;
    //加载状态
    BOOL loadMore;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    CGFloat width = self.view.frame.size.width;
    CGFloat height = self.view.frame.size.height;
    
    //创建Table
    tableView = [[UITableView alloc] initWithFrame:(CGRect){0,20,width,height}];
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:tableView];

    [self readySource];
    
    //创建底部表格
    [self creatFooterView];
}

//数据资源
- (void)readySource{

    dataArray = [[NSMutableArray alloc] initWithObjects:@"数学", @"语文", @"英语", @"历史", @"地理", @"政治", @"物理", @"化学", @"生物", @"体育", @"音乐", @"美术", nil];
    moreArray = [[NSMutableArray alloc] initWithObjects:@"Math", @"Chinese", @"English", @"History", nil];
}

//返回分组个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

//返回每个分组中的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)TableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //建立可重用标识符
    static NSString *indentifier = @"UITableViewCell";
    
    //    NSString *indentifier = [NSString stringWithFormat:@"UITableViewCell%ld%ld",(long)indexPath.row,(long)indexPath.section];
    
    UITableViewCell *cell = [TableView dequeueReusableCellWithIdentifier:indentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
    }
    
    //移除所有子视图
    [cell.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        UIView *view = (UIView*)obj;
        [view removeFromSuperview];
    }];
    
    //添加新视图
    UILabel *title = [[UILabel alloc] initWithFrame:(CGRect){20,10,200,30}];
    NSString *str = [dataArray objectAtIndex:indexPath.row];
    title.text = str;
    title.font = [UIFont systemFontOfSize:20];
    title.textColor = [UIColor blueColor];
    [cell addSubview:title];
    
    return cell;
}

//设置TableViewCell行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}

//开始加载
- (void)loadDataBegin{
    if (loadMore == NO) {
        
        loadMore = YES;
        //设置滚动轮
        UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc] initWithFrame:(CGRect){150,30,30,30}];
        [activity setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
        [tableView.tableFooterView addSubview:activity];
        [activity startAnimating];
        
        [self loading];
    }

}

//结束拖拽时调用得方法
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{

    //设置范围,下拉到最底部时开始加载数据
    if (!loadMore &&scrollView.contentOffset.y>(scrollView.contentSize.height-self.view.frame.size.height)) {
        [self loadDataBegin];
    }
}

//加载数据中
- (void)loading{
    dataNumber = [dataArray count];
    
    for (int i = 0; i < moreArray.count; i++) {
        [dataArray addObject:[moreArray objectAtIndex:i]];
    }
    
    [tableView reloadData];
    [self loadDataEnd];
}

//结束加载
- (void)loadDataEnd{

    loadMore = NO;
    //结束加载创建底部表格
    [self creatFooterView];
}

//创建底部表格
- (void)creatFooterView{
    tableView.tableFooterView = nil;
    
    UIView *footerView = [[UIView alloc] initWithFrame:(CGRect){0,0,self.view.frame.size.width,50}];
    footerView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
    
    UILabel *title = [[UILabel alloc] initWithFrame:(CGRect){0,15,self.view.frame.size.width,20}];
    title.text = @"上拉显示更多数据";
    title.font = [UIFont systemFontOfSize:15];
    title.textAlignment = NSTextAlignmentCenter;
    [footerView addSubview:title];
    
    tableView.tableFooterView = footerView;

}

 

转载于:https://www.cnblogs.com/0320y/p/5105028.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值