UITableView實現上拉加載更多

// 創建表格底部
- (void) createTableFooter
{
   
    myTableView.tableFooterView = nil;
   
    UIView *tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, myTableView.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];
    
    myTableView.tableFooterView = tableFooterView;  
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    // 下拉到最底部時顯示更多數據
   
   
if(!_loadingMore && scrollView.contentOffset.y > ((scrollView.contentSize.height - scrollView.frame.size.height)))
       
    {
       
NSLog(@"開始加載");
       
        [
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];
       
        [
searchTableView.tableFooterView addSubview:tableFooterActivityIndicator];
       
       
NSLog(@"正在加載");
       
        [
self loadDataing];
       
    }
   
}

// 加載數據中
- (void) loadDataing
{
   /**
     *要處理的事件
     */
     [self loadDataEnd];
}


// 加載數據完畢
- (void) loadDataEnd
{
   
NSLog(@"加載完畢");
   
   
_loadingMore = NO;
   
    [
self createTableFooter];
   
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章