下拉刷新、上拉加载更多

1、系统控件UIRefreshControl

使用方法:

只对UITableviewController有用;
不能上拉刷新;
init或者viewdidload中创建UIRefreshControl,设置文字,颜色等信息;
系统自动管理UIRefreshControl,自动添加到tableview视图中;
给UIRefreshControl添加方法,当值改变的时候调用,方法用于数据请求;
该方法中请求数据确认完成之后,调用endRefreshing方法,关闭刷新;
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupRefresh];
}

/**
 *  集成下拉刷新
 */
-(void)setupRefresh
{
    //1.添加刷新控件
    UIRefreshControl *control=[[UIRefreshControl alloc]init];
    control.attributedTitle = [[NSAttributedString alloc] initWithString:@"努力加载中……"];
    control.tintColor = [UIColor grayColor];
    [control addTarget:self action:@selector(refreshStateChange:) forControlEvents:UIControlEventValueChanged];
    [self.tableView addSubview:control];
    
    //2.马上进入刷新状态,并不会触发UIControlEventValueChanged事件
    [control beginRefreshing];
    
    // 3.加载数据
    [self refreshStateChange:control];
}

/**
 *  UIRefreshControl进入刷新状态:加载最新的数据
 */
-(void)refreshStateChange:(UIRefreshControl *)control
{
    
    NSLog(@"刷新了");
    [control endRefreshing];
}

@end
View Code

效果图

原文地址:https://www.cnblogs.com/imChay/p/5591573.html