第一次写文章,写的不好地方还请广大朋友指正!
先看效果图:
上图就是实现了一个分页加载的刷新及根据输入的关键字实现实时刷新,在加载时是分页加载的,有两种方式刷新,一种是手动上拉刷新,第一种根据滑动的位置实现智能预加载!效果图中是用的第一种。
废话不多上直接上代码:
typedef NS_ENUM(NSUInteger, RequestType){
requestAll = 1000, //加载所有的列表
requestKey = 1001,//输入关键字时
requestPull = 1002 //上拉时
};
@interface MyBookListViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
@property (nonatomic,strong) NSArray *dataArray;
@property (nonatomic,strong) NSMutableArray *modelDataArray;
@property (weak, nonatomic) IBOutlet UITableView *bookListTableView;
@property (nonatomic,assign) int pageSize;
@property (nonatomic,assign) int pageNo;//页码,注意页码的变化
@property (nonatomic,assign) NSInteger totalCount;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UITextField *keywordTextField;
@property (nonatomic,strong) NSString *keyword;
@property (nonatomic,assign) RequestType requestType;
@end
@implementation MyBookListViewController
#pragma mark -----Getters
-(NSMutableArray *)modelDataArray{
if (!_modelDataArray) {
_modelDataArray = [NSMutableArray array];
}
return _modelDataArray;
}
-(NSArray *)dataArray{
if (!_dataArray) {
_dataArray = [NSArray array];
}
return _dataArray;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.navigationBar.backgroundColor = navColor;
self.navigationController.navigationBar.barTintColor = navColor;
self.title = @"通讯录";
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
self.requestType = requestAll;
__weak typeof (self) weakSelf = self;
if (self.keywordTextField.text.length == 0) {
self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
if (self.keywordTextField.text.length == 0) {
self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
weakSelf.pageNo++ ;
if (self.keywordTextField.text.length == 0) {
[weakSelf loadListDataWithKeyWord:@"" withRequestType:requestPull withIsShowMessage:NO];
}else{
[weakSelf loadListDataWithKeyWord:self.keywordTextField.text withRequestType:requestPull withIsShowMessage:NO];
}
}];
}
}];
}
self.pageNo = 1;
self.pageSize = 20;
[self loadListDataWithKeyWord:@"" withRequestType:requestAll withIsShowMessage:YES];
self.bookListTableView.delegate = self;
self.bookListTableView.dataSource = self;
[self.bookListTableView reloadData];
[self.bookListTableView registerNib:[UINib nibWithNibName:@"MybookListTableViewCell" bundle:nil] forCellReuseIdentifier:reusedIdentifier];
self.keywordTextField.placeholder= @"查询关键字";
[self.keywordTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}
#pragma mark -----请求加载通讯录
-(void)loadListDataWithKeyWord:(NSString *)keyword withRequestType:(RequestType)requestType withIsShowMessage:(BOOL)isShowMessage{
if (isShowMessage) {
[MBProgressHUD showMessage:@"正在加载"];
}
//请求参数
NSDictionary *dict = @{};
[NetworkUtils postDictWithUrl:@"你的url" parameters:dict success:^(id responseObject) {
[self.tableView.mj_footer endRefreshing];
[MBProgressHUD hideHUD];
NSInteger result = [[(NSDictionary *)responseObject objectForKey:@"status"] integerValue];
switch (result) {
case 0:{
[MBProgressHUD showError:@"请求失败,请返回重新操作!"];
}
case 1:{
self.totalCount = [[responseObject objectForKey:@"totRecord"] integerValue];
NSMutableArray *newArray = [NSMutableArray arrayWithCapacity:3];
self.dataArray = [responseObject objectForKey:@"data"];
for (NSDictionary *detailDict in self.dataArray) {
MybookListModel *myBookListModel = [[MybookListModel alloc] init];
[newArray addObject:myBookListModel];
}
if (requestType == requestAll) {
[self.modelDataArray removeAllObjects];
[self.modelDataArray addObjectsFromArray:newArray];
}else if (requestType == requestKey){
[self.modelDataArray removeAllObjects];
[self.modelDataArray addObjectsFromArray:newArray];
}else{
[self.modelDataArray addObjectsFromArray:newArray];
}
if (self.modelDataArray.count < 20 || newArray.count< 20) {
[self.tableView.mj_footer endRefreshingWithNoMoreData];
}
if (isShowMessage) {
[MBProgressHUD hideHUD];
}
[self.tableView reloadData];
break;
}
default:
break;
}
} fail:^{
[MBProgressHUD showError:@"请求失败!"];
}];
}
#pragma mark -----DataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.modelDataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *ID = @"booklistCell";
MybookListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[[NSBundle mainBundle]loadNibNamed:@"MybookListTableViewCell" owner:nil options:nil]lastObject];
}
MybookListModel *booklistModel = self.modelDataArray[indexPath.row];
cell.myBookListModel = booklistModel;
return cell;
}
#pragma mark -----UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 40.0;
}
#pragma mark -----查询按钮点击
- (IBAction)queryClick:(UIButton *)sender {
self.requestType = requestKey;
self.pageNo = 1;
[self loadListDataWithKeyWord:self.keywordTextField.text withRequestType:requestKey withIsShowMessage:YES];
}
#pragma mark ------UITextFieldDelegate
-(void)textFieldDidEndEditing:(UITextField *)textField{
self.keywordTextField.text = textField.text;
}
- (void) textFieldDidChange:(id) sender {
UITextField *_field = (UITextField *)sender;
//当使用系统自带的键盘会出现问题,下面几行代码就是解决此bug
UITextRange *selectedRange = [_field markedTextRange];
NSString *newText = [_field textInRange:selectedRange];
if (newText.length>0) {
return;
}
if (_field.text.length == 0) {
if (self.pageNo != 1) {
self.pageNo = 1;
}
self.requestType = requestAll;
self.keywordTextField.text = _field.text;
[self loadListDataWithKeyWord:self.keywordTextField.text withRequestType:requestAll withIsShowMessage:NO];
}else{
if (self.pageNo != 1) {
self.pageNo = 1;
}
self.requestType = requestKey;
self.keywordTextField.text = _field.text;
[self loadListDataWithKeyWord:self.keywordTextField.text withRequestType:requestKey withIsShowMessage:NO];
}
}
@end
第二种加载刷新方式是实现智能预加载,这里有介绍更详细的方案,我就不复制粘贴了,请点击此处
网友评论