上拉显示更多数据

ViewController.h

 

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate> {
    //表格数据数组
    NSMutableArray *tableData;
    //加载更多时显示的数据
    NSMutableArray *tableMoreData;
    //加载状态
    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

 

ViewController.m

 

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@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

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [tableData count];
}

- (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 {
    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 {
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {

}

- (void)dealloc {
    [super dealloc];
}

@end

猜你喜欢

转载自eric-gao.iteye.com/blog/1584539