簡單實現---上拉加載刷新---

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


發佈了63 篇原創文章 · 獲贊 3 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章