iOS UITableView 网络请求刷新 搜索
2015-09-24 11:20
239 查看
- (void)createDataScource { if (_isPull) { [_dataSource removeAllObjects]; } AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager]; manager.responseSerializer = [AFHTTPResponseSerializer serializer]; [manager GET:[NSString stringWithFormat:RequestURL,_page] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { NSDictionary * rootDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil]; NSDictionary * data = rootDic[@"data"]; NSArray * list = data[@"lists"]; for (NSDictionary * dic in list) { [_dataSource addObject:dic]; } [_tableView reloadData]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { }]; } - (void)createSearch { _searchController = [[UISearchController alloc] initWithSearchResultsController:nil]; // 是否隐藏导航栏 _searchController.hidesNavigationBarDuringPresentation = YES; _searchController.dimsBackgroundDuringPresentation = NO; [_searchController.searchBar sizeToFit]; _tableView.tableHeaderView = _searchController.searchBar; _searchController.searchResultsUpdater = self; } - (void)createRefresh { [_tableView addHeaderWithTarget:self action:@selector(pull)]; [_tableView addFooterWithTarget:self action:@selector(push)]; } - (void)pull { _isPull = YES; _page = 1; [self createDataScource]; [_tableView headerEndRefreshing]; } - (void)push { _isPull = NO; _page ++; [self createDataScource]; [_tableView footerEndRefreshing]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (_searchController.active) { return (_resultArray.count-1)/2+1; }else{ return (_dataSource.count-1)/2+1; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { LYNewTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:[LYNewTableViewCell identifier]]; if (!cell) { NSArray * objects = [[NSBundle mainBundle] loadNibNamed:@"LYNewTableViewCell" owner:nil options:nil]; cell = objects[0]; } if (_searchController.active) { NSInteger num = indexPath.row * 2;// 每次跳2个位置 NSMutableArray * array = [NSMutableArray array]; for (int i = 0; i < 2; i ++) { if (num + i < _resultArray.count) { [array addObject:_resultArray[num + i]]; } } [cell refreshUI:array]; }else{ NSInteger num = indexPath.row * 2;// 每次跳2个位置 NSMutableArray * array = [NSMutableArray array]; for (int i = 0; i < 2; i ++) { // 不满足则不往数组里加 if (num + i < _dataSource.count) { [array addObject:_dataSource[num + i]]; } } [cell refreshUI:array]; } return cell; } - (void)updateSearchResultsForSearchController:(UISearchController *)searchController { if (_resultArray != nil) { [_resultArray removeAllObjects]; }else{ _resultArray = [[NSMutableArray alloc] init]; } for (NSDictionary * dic in _dataSource) { NSRange range = [dic[@"name"] rangeOfString:_searchController.searchBar.text]; if (range.location != NSNotFound) { [_resultArray addObject:dic]; } } [_tableView reloadData]; }
相关文章推荐
- Nginx 的 TCP 负载均衡介绍
- HTTP请求中文等出现乱码解决方案
- HTTP协议详解
- HttpClient多线程调用并返回值
- java学习笔记网络编程
- html添加网络音乐
- 从头开始实现神经网络——入门
- 网络编程
- tcpdump抓包分析详解
- 基于AFNetWoring 封装的网络请求类
- 一只简单的网络爬虫(基于linux C/C++)————线程相关
- 【XCode7+iOS9】http网路连接请求、MKPinAnnotationView自定义图片和BitCode相关错误
- 七层网络模型,协议,状态码认识
- python之web模块学习-- httplib
- http 状态码
- HttpServletRequest对象方法的用法
- Nmcli 网络管理命令行工具基础
- HAproxy 简单测试(http与tcp代理)
- nyoj 120 校园网络 【强连通】
- HttpServletRequest.getRemoteAddr()之代理提交