您的位置:首页 > 理论基础 > 计算机网络

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];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: