MJRefresh

2017年3月24日
一.实现tableView简单分页效果
1.效果


MJRefresh_第1张图片
Paste_Image.png

2.实现

#define onePageSize (20)

//分页拉取方向
typedef NS_ENUM(NSInteger, HuPullDirection){
    HuPullDirectionNoPull,//首次不拉(可以不用)
    HuPullDirectionHeader,//向上拉
    HuPullDirectionFooter,//向下拉
};

//  HuSearchNurseTrainingViewController.m
@interface HuNurseTrainingViewSearchController()
{
    UITableView *_searchTableView;//当前搜索表格
    NSMutableArray *_searchPresent;

    NSInteger _pageNum;// 分页
}

@implementation HuNurseTrainingViewSearchController

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

- (void)initData
{
    _searchPresent = @[].mutableCopy;
}

- (void)initSearchTableView
{
    if (_searchTableView == nil) {
        UITableView *tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, HHBWIDTH, NT_searchViewController_tableview_height) style:UITableViewStylePlain];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.rowHeight = [HuNurseTrainningSearchTableViewCell cellHeight];
        tableView.backgroundColor = fontcolor_C1;
        [tableView registerClass:[HuNurseTrainningSearchTableViewCell class] forCellReuseIdentifier:[HuNurseTrainningSearchTableViewCell identifier:HuNurseTrainningSearchCellTypePresent]];
        [HuConfigration setExtraCellLineHidden:tableView];


        //分页设置
        tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            _pageNum = 1;
            [self onPullUpRefresh];
        }];
        tableView.mj_footer=[MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            _pageNum ++;
            [self onPullDownRefresh];
        }];


        _searchTableView = tableView;
        [self.view addSubview: _searchTableView];

    }
}

- (void)onPullUpRefresh
{
    [self searchNTClasses:_searchText withDirection:HuPullDirectionHeader];
}

- (void)onPullDownRefresh
{
    [self searchNTClasses:_searchText withDirection:HuPullDirectionFooter];
}

//请求课程接口,同事回调刷新页面 新接口不支持分页
- (void)searchNTClasses:(NSString*)searchText withDirection:(HuPullDirection)direct
{
    NSDictionary *params = @{@"accountId":NurseId,
                             @"type":@"1",
                             @"hospitalId":HospitalId,
                             @"courseName":searchText,
                             @"pageNum":@(_pageNum),
                             @"pageSize":@(onePageSize)};
    NSString *funItem = HuFuncItem_searchNTCourse_get;
    WS(weakSelf);
    if(direct == HuPullDirectionNoPull) //页面首次加载  暂时不用
    {
        //暂时不用用 这个方法替代[_searchTableView.mj_header beginRefreshing];
    }
    else if(direct == HuPullDirectionHeader) //向上拉
    {
        [Request requestAppServerNT_Get:params funItem:funItem success:^(NSDictionary *dic) {
            [_searchPresent removeAllObjects];

            UITableView *tableView = _searchTableView;
            NSArray *newArray = [TrainListModel mj_objectArrayWithKeyValuesArray:dic[@"data"][@"result"]];
            NSInteger count = newArray.count;
            if (count > 0) {
                tableView.mj_footer.hidden = NO;
                [_searchPresent addObjectsFromArray:newArray];
            }else{
                tableView.mj_footer.hidden = YES; //没有数据
            }
            [tableView reloadData];  //一定要先reload
            [tableView.mj_header endRefreshing];


            //添加默认图片
            [weakSelf dealDefaultView:count];

        } error:^(NSDictionary *dic) {
            TRACELOG(@"%@",dic[@"errMsg"] ? dic[@"errMsg"] : @"网络异常");
        } failure:^{
            TRACELOG(@"failure");
        }];

    }
    else if (direct == HuPullDirectionFooter)  //向下拉
    {
        [Request requestAppServerNT_Get:params funItem:funItem success:^(NSDictionary *dic) {
            UITableView *tableView = _searchTableView;
            NSArray *newArray = [TrainListModel mj_objectArrayWithKeyValuesArray:dic[@"data"][@"result"]];
            if (newArray.count < onePageSize) {
                tableView.mj_footer.hidden = YES;
            }
            [_searchPresent addObjectsFromArray:newArray];
            [tableView reloadData];
            [tableView.mj_footer endRefreshing];

        } error:^(NSDictionary *dic) {
            TRACELOG(@"%@",dic[@"errMsg"] ? dic[@"errMsg"] : @"网络异常");
        } failure:^{
            TRACELOG(@"failure");
        }];
    }

}

- (void)dealSearhClassProduce:(NSString *)searchText
{
    if (searchText.length > 0) {
        [self initSearchTableView];
        [_searchTableView.mj_header beginRefreshing];
    }
}

#pragma mark - textField delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField.text.length > 0) {
        _searchText = textField.text;
        [self dealSearhClassProduce:_searchText];

    }

    return YES;
}

如果您发现本文对你有所帮助,如果您认为其他人也可能受益,请把它分享出去。

你可能感兴趣的:(MJRefresh)