博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UITableView底部FooterView实现上拉刷新
阅读量:4198 次
发布时间:2019-05-26

本文共 3978 字,大约阅读时间需要 13 分钟。

转载自:

 

@interface FooterViewTestViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>{

// 表格数据数组,因为是演示代码,直接定义为数组

NSMutableArray *tableData;

    // 下拉时显示的数据

NSMutableArray *tableMoreData;

    // 数据数量

NSUInteger dataNumber;

    // 加载状态

BOOL _loadingMore;

 

UITableView *table;

}

@property (nonatomic, retain) UITableView *table;

@property (nonatomic, retain) NSMutableArray *tableData;

@property (nonatomic, retain) NSMutableArray *tableMoreData;

// 创建表格底部

- (void) createTableFooter;

// 开始加载数据

- (void) loadDataBegin;

// 加载数据中

- (void) loadDataing;

// 加载数据完毕

- (void) loadDataEnd;

@end

 

@implementation FooterViewTestViewController

@synthesize table;

@synthesize tableData;

@synthesize tableMoreData;

- (void)viewDidLoad {

    [super viewDidLoad];

table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460) style:UITableViewStylePlain];

table.delegate = self;

table.dataSource = self;

[self.view addSubview:table];

 

tableData = [[NSMutableArray alloc] initWithObjects:

@"January",@"February",@"March",@"April",@"May",@"June",

@"July",@"August",@"September",@"October",@"November",@"December",nil];

tableMoreData = [[NSMutableArray alloc] initWithObjects:@"BAIDU",@"GOOGLE",@"FACEBOOK",@"YAHOO",nil];

 

[self createTableFooter];

}

#pragma mark -

#pragma mark Table view data source

// Customize the number of sections in the table view.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;

}

// Customize the number of rows in the table view.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [tableData count];

}

// Customize the appearance of table view cells.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    

    static NSString *CellIdentifier = @"Cell";

    

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    }

    

cell.textLabel.text = [tableData objectAtIndex:indexPath.row];

 

    return cell;

}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate

{    

    // 下拉到最底部时显示更多数据

if(!_loadingMore && scrollView.contentOffset.y > ((scrollView.contentSize.height - scrollView.frame.size.height)))

{

[self loadDataBegin];

}

}

// 开始加载数据

- (void) loadDataBegin

{

    if (_loadingMore == NO) 

    {

        _loadingMore = YES;

        UIActivityIndicatorView *tableFooterActivityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(75.0f, 10.0f, 20.0f, 20.0f)];

        [tableFooterActivityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];

        [tableFooterActivityIndicator startAnimating];

        [self.table.tableFooterView addSubview:tableFooterActivityIndicator];

 

[self loadDataing];

    }

}

// 加载数据中

- (void) loadDataing

{

dataNumber = [tableData count];

 

for (int x = 0; x < [tableMoreData count]; x++) 

{

[tableData addObject:[tableMoreData objectAtIndex:x]];

}

 

[[self table] reloadData];

 

[self loadDataEnd];

}

// 加载数据完毕

- (void) loadDataEnd

{

_loadingMore = NO;

[self createTableFooter];

}

// 创建表格底部

- (void) createTableFooter

{

    self.table.tableFooterView = nil;

    UIView *tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.table.bounds.size.width, 40.0f)]; 

    UILabel *loadMoreText = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 116.0f, 40.0f)];

    [loadMoreText setCenter:tableFooterView.center];

    [loadMoreText setFont:[UIFont fontWithName:@"Helvetica Neue" size:14]];

    [loadMoreText setText:@"上拉显示更多数据"];

    [tableFooterView addSubview:loadMoreText];    

 

    self.table.tableFooterView = tableFooterView;

}

- (void)didReceiveMemoryWarning {

// Releases the view if it doesn't have a superview.

    [super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.

}

- (void)viewDidUnload {

// Release any retained subviews of the main view.

// e.g. self.myOutlet = nil;

}

- (void)dealloc {

    [super dealloc];

}

@end

 

你可能感兴趣的文章
交大研究生,就一个字牛
查看>>
Android开发之ADB使用
查看>>
Android模拟器使用模拟SD卡
查看>>
Redis入门教程
查看>>
Google code上利用SVN托管代码
查看>>
技术的发展与互联网的发展
查看>>
工作流简介
查看>>
软件项目管理实践之日计划
查看>>
浅析大型网站的架构
查看>>
电脑故障排除经验
查看>>
数据库升级脚本制作
查看>>
HTTP response splitting 攻击
查看>>
Google十三年
查看>>
SSL原理
查看>>
预编译头sadafx.h原理
查看>>
DLL编写教程
查看>>
WebService WSDL详解(上)
查看>>
WebService WSDL详解(下)
查看>>
公司旅游--金华武义二日游
查看>>
虚拟机四种网络连接模式比较
查看>>