简单实现---上拉加载刷新---

ROOTViewController直接设置为跟视图控制器.APPdelegate中的代码我就不贴了.一下为.h中的文件

代码注释已经非常清楚,其中所说的菊花就是动态的刷新图.


#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
{
    //表格数据
    NSMutableArray *tableData;
    //上拉时添加的数组内的数据
    NSMutableArray *tableMoreData;
    //数据数量
    NSUInteger dataNumber;
    //加载状态
    BOOL _loadingMore;
}
@property(nonatomic,retain)UITableView *table;

//创建盛放菊花的视图,也可以侦测上拉
- (void) createTableFooter;
//开始加载数据
- (void) loadDataBegin;
- (void) loadDataing;
- (void) loadDataEnd;
@end

以下为.m中的文件

#import "RootViewController.h"

@interface RootViewController ()

@end


@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.view.backgroundColor = [UIColor orangeColor];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 320, 460) style:UITableViewStylePlain];
    self.table.backgroundColor = [UIColor orangeColor];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    tableData = [[NSMutableArray alloc] initWithObjects:
                 @"1111",@"2222",@"3333",@"4444",@"5555",@"5555",@"5555",@"5555",@"5555",@"5555",@"5555",nil];
    tableMoreData = [[NSMutableArray alloc] initWithObjects:@"新家1",@"新家2",@"新家3",@"新家4",nil];
    
    [self createTableFooter];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (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];
    }
    cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
    return cell;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    //下拉到最底部的时候80为侦测的幅度像素,越小越灵敏,但是不能小于0;
    if (!_loadingMore && scrollView.contentOffset.y > (scrollView.contentSize.height-scrollView.frame.size.height +80)) {
        [self loadDataBegin];
    }
    
}

- (void)loadDataBegin
{
    
    //if (_loadingMore == NO) {
        _loadingMore = YES;
       
        [self loadDataing];
   // }
}

//将新数组添加到我们的数据数组中
- (void) loadDataing
{
    dataNumber = [tableData count];
    for (int x = 0; x < [tableMoreData count]; x++) {
        [tableData addObject:[tableMoreData objectAtIndex:x]];
    }
    [self.table reloadData];
    [self createTableFooter];
    [self loadDataEnd];
}

- (void) loadDataEnd
{
    _loadingMore = NO;
}
- (void)createTableFooter
{
    UIView *tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.table.bounds.size.width, 30)];
    //创建刷新提示文字并设置字体
    UILabel *loadMoreText = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 116, 40)];
    [loadMoreText setCenter:tableFooterView.center];
    [loadMoreText setFont:[UIFont fontWithName:@"Helvetica Neue" size:14]];
    [loadMoreText setText:@"上拉加载更多"];
    [tableFooterView addSubview:loadMoreText];
    self.table.tableFooterView = tableFooterView;
    
    //此处为动态菊花
    UIActivityIndicatorView *tableFootActivityIndicatorView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(75, 10, 20, 20)];
    [tableFootActivityIndicatorView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [tableFootActivityIndicatorView startAnimating];
    [self.table.tableFooterView addSubview:tableFootActivityIndicatorView];
    
}


- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

@end


简单实现---上拉加载刷新---

上一篇:ssh命令 安装 Vbox以及创建虚拟机等


下一篇:简单实现---下拉刷新 --使用UITableViewController中的refreshControl属性