当前位置: 首页 > 工具软件 > MJRefresh > 使用案例 >

MJRefresh类------>下拉刷新,上拉加载

唐星晖
2023-12-01

@MJ大神出品,很方便的实现下拉刷新,上拉加载数据,下载地址:http://code4app.com/ios/快速集成下拉上拉刷新/52326ce26803fabc46000000


@导入类

1.将MJRefresh文件夹整个导入所需的工程中。(支持ARC,MRC工程要注意设置)

2.文件夹中的MJRefresh.bundle—>arrow@2x.png 图片可自行替换,这个会在刷新过程中显示。

3.可以在MJRefreshConst.h和MJRefreshConst.m文件中自定义显示的文字内容和文字颜色。

4.只要你的view能够滚动,就能集成这个控件,比如UIScrollView、UITableView、UICollectionView

5  导入头文件   #import "MJRefresh.h"


@实现

     MJ大神提供了代理协议方法<MJRefreshBaseViewDelegate>和Block方法,大家随便选一种实现就行

下面给出代理协议方法实现的主要代码:

      主要实现:上拉加载10条新闻,每次都是显示在最前面;下拉加载10条新闻,每次都是显示在已有新闻下面

      @自动刷新:调用beginRefreshing可以自动进入下拉刷新状态

#import "NewsListViewController.h"
#import "HMTMyCustomNetRequest.h"
#import "NewsItem.h"
#import "MJRefresh.h"

@interface NewsListViewController () <MJRefreshBaseViewDelegate>
{
    HMTMyCustomNetRequest * _request;
    MJRefreshHeaderView * _header;
    MJRefreshFooterView * _footer;

    NSUInteger number;
    
}
@property (nonatomic,retain)NSMutableArray * newDataArray;
@property (nonatomic,retain)NSMutableArray * oldDataArray;
@property (nonatomic,copy)NSString * dataString;

@end

@implementation NewsListViewController
- (void)dealloc
{
    
    [_header release];
    [_footer release];
    
    RELEASE_SAFELY(_dataString);
    RELEASE_SAFELY(_newDataArray);
    RELEASE_SAFELY(_oldDataArray);
    
    //取消网络请求
    [_request cancelNSURLConnection];
    _request = nil;
    
    [super dealloc];
}
- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    number = 0;
    
#pragma mark 从顶部往下刷新------>下拉刷新
    _header = [MJRefreshHeaderView header];
    _header.scrollView = self.tableView;
    _header.delegate = self;
    
#pragma mark 从底部往上刷新------>上拉刷新
    _footer = [MJRefreshFooterView footer];
    _footer.scrollView = self.tableView;
    _footer.delegate = self;
    
    self.newDataArray = [NSMutableArray array];
    self.oldDataArray = [NSMutableArray array];

}

#pragma mark - 开始刷新
- (void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView{

    self.dataString = [[NSString stringWithFormat:@"date=20131129&startRecord=%lu",number] stringByAppendingString:@"&len=10&udid=1234567890&terminalType=Iphone&cid=213"];
    
    _request = [[HMTMyCustomNetRequest alloc] init];
    NSData * postData = [_dataString dataUsingEncoding:NSUTF8StringEncoding];
    [_request requestForPOSTWithUrl:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx" postData:postData];
    
    [_request setFinishLoadBlock:^(NSData * data){
        
        //解析数据
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray * originalArray = [dic objectForKey:@"news"];
    
        //将originalArray中的字典,逐个封装为newsItem数据对象,存储在UITableView的数据源中
        for (int i = 0; i<[originalArray count]; i++) {
            
            //从解析得到的数组中,获取包含新闻信息的字典对象并把它封装为NewsItem类型的数据对象
            NewsItem * item = [[NewsItem alloc] initWithDic:[originalArray objectAtIndex:i]];
            
            //将封装好数据对象item添加到tableView的数据源中
            [_newDataArray addObject:item];
            [item release];
            
        }
        
#pragma mark 下拉刷新,要的是加载新数据---见网易新闻刷新
        if ([refreshView isMemberOfClass:[MJRefreshHeaderView class]]) {
        
            for (int i = 0; i < [_newDataArray count]; i++) {
                
                [self.oldDataArray insertObject:[_newDataArray objectAtIndex:i] atIndex:i];
                
            }

            [self.tableView reloadData];
            // 结束刷新
            [refreshView endRefreshing];
         
#pragma mark 上拉加载,要的是加载更多数据---见百度贴吧翻页
        }else if([refreshView isMemberOfClass:[MJRefreshFooterView class]]){
        
           
            for (int i = 0; i < [_newDataArray count]; i++) {
                
                [self.oldDataArray addObject:[_newDataArray objectAtIndex:i]];
                
            }
            
            [self.tableView reloadData];
            // 结束刷新
            [refreshView endRefreshing];

        }
        
    }];
    
}

#pragma mark - 刷新结束时调用的方法
- (void)refreshViewEndRefreshing:(MJRefreshBaseView *)refreshView{

    // 下拉刷新
    if ([refreshView isMemberOfClass:[MJRefreshHeaderView class]]) {
        
        self.newDataArray = [NSMutableArray array];
        number += 10;
        
    } else {
        
        self.newDataArray = [NSMutableArray array];
        number += 10;
        
    }
    
}

#pragma mark - 监测状态
- (void)refreshView:(MJRefreshBaseView *)refreshView stateChange:(MJRefreshState)state{

    switch (state) {
        case MJRefreshStateNormal:
            NSLog(@"%@----切换到:普通状态", refreshView.class);
            
            break;
            
        case MJRefreshStatePulling:
            NSLog(@"%@----切换到:松开即可刷新的状态", refreshView.class);
            break;
            
        case MJRefreshStateRefreshing:
            NSLog(@"%@----切换到:正在刷新状态", refreshView.class);
            break;
        default:
            break;
    }

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return [_oldDataArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewsItem * item = [_oldDataArray objectAtIndex:indexPath.row];
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%lu %@",indexPath.row+1,item.newsTitle];
    return cell;
}

@end


Block实现的主要代码:

#pragma mark 下拉刷新,要的是加载新数据---见网易新闻刷新
- (void)refreshViewOfBlock{

    _header = [MJRefreshHeaderView header];
    _header.scrollView = self.tableView;
    
    [_header setBeginRefreshingBlock:^(MJRefreshBaseView * refreshView){
        
        self.dataString = [[NSString stringWithFormat:@"date=20131129&startRecord=%lu",number] stringByAppendingString:@"&len=10&udid=1234567890&terminalType=Iphone&cid=213"];
        
        _request = [[HMTMyCustomNetRequest alloc] init];
        NSData * postData = [_dataString dataUsingEncoding:NSUTF8StringEncoding];
        [_request requestForPOSTWithUrl:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx" postData:postData];
        
        [_request setFinishLoadBlock:^(NSData * data){
            
            //解析数据
            NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSArray * originalArray = [dic objectForKey:@"news"];
            
            //将originalArray中的字典,逐个封装为newsItem数据对象,存储在UITableView的数据源中
            for (int i = 0; i<[originalArray count]; i++) {
                
                //从解析得到的数组中,获取包含新闻信息的字典对象并把它封装为NewsItem类型的数据对象
                NewsItem * item = [[NewsItem alloc] initWithDic:[originalArray objectAtIndex:i]];
                
                //将封装好数据对象item添加到tableView的数据源中
                [_DataArray addObject:item];
                [item release];
                
            }
            
            for (int i = 0; i < [_DataArray count]; i++) {
                
                [self.oldDataArray insertObject:[_DataArray objectAtIndex:i] atIndex:i];
                
            }

            [self.tableView reloadData];
            // 结束刷新
            [refreshView endRefreshing];
            
        }];
    }];
    [_header setEndStateChangeBlock:^(MJRefreshBaseView * refreshView){
        NSLog(@"11111");
        self.DataArray = [NSMutableArray array];
        number += 10;
        
    }];
    _header.refreshStateChangeBlock = ^(MJRefreshBaseView *refreshView, MJRefreshState state) {
        // 控件的刷新状态切换了就会调用这个block
        switch (state) {
            case MJRefreshStateNormal:
                NSLog(@"%@----切换到:普通状态", refreshView.class);

                break;
                
            case MJRefreshStatePulling:
                NSLog(@"%@----切换到:松开即可刷新的状态", refreshView.class);
                break;
                
            case MJRefreshStateRefreshing:
                NSLog(@"%@----切换到:正在刷新状态", refreshView.class);
                break;
            default:
                break;
        }
    };
}



 类似资料: