IOS详解TableView——内置刷新,EGO,以及搜索显示控制器
IOS详解TableView——内置刷新,EGO,以及搜索显示控制器
- /******内置刷新的常用属性设置******/
- UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
- refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"下拉刷新"];
- refresh.tintColor = [UIColor blueColor];
- [refresh addTarget:self action:@selector(pullToRefresh) forControlEvents:UIControlEventValueChanged];
- self.refreshControl = refresh;
- //下拉刷新
- - (void)pullToRefresh
- {
- //模拟网络访问
- [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
- self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"刷新中"];
- double delayInSeconds = 1.5;
- dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
- dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
- _rowCount += 5;
- [self.tableView reloadData];
- //刷新结束时刷新控件的设置
- [self.refreshControl endRefreshing];
- self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"下拉刷新"];
- [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
- _bottomRefresh.frame = CGRectMake(0, 44+_rowCount*RCellHeight, 320, RCellHeight);
- });
- }
- /******自定义查看更多属性设置******/
- _bottomRefresh = [UIButton buttonWithType:UIButtonTypeCustom];
- [_bottomRefresh setTitle:@"查看更多" forState:UIControlStateNormal];
- [_bottomRefresh setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
- [_bottomRefresh setContentEdgeInsets:UIEdgeInsetsMake(15, 0, 0, 0)];
- [_bottomRefresh addTarget:self action:@selector(upToRefresh) forControlEvents:UIControlEventTouchUpInside];
- _bottomRefresh.frame = CGRectMake(0, 44+_rowCount*RCellHeight, 320, RCellHeight);
- [self.tableView addSubview:_bottomRefresh];
- //上拉加载
- - (void)upToRefresh
- {
- _bottomRefresh.enabled = NO;
- [SVProgressHUD showWithStatus:@"加载中..."];
- [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
- double delayInSeconds = 1.5;
- dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
- dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
- _rowCount += 5;
- [self.tableView reloadData];
- [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
- [SVProgressHUD showSuccessWithStatus:@"加载完成"];
- _bottomRefresh.frame = CGRectMake(0, 44+_rowCount*RCellHeight, 320, RCellHeight);
- _bottomRefresh.enabled = YES;
- });
- }
- /******头部的下拉刷新******/
- _headerRefreshView = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0, 0 - self.tableView.frame.size.height, 320, self.tableView.frame.size.height) withType:EGORefreshHeader];
- _headerRefreshView.delegate = self;
- [self.tableView addSubview:_headerRefreshView];
- #pragma mark -
- #pragma mark EGORefresh Delegate
- - (BOOL)egoRefreshTableDataSourceIsLoading:(UIView *)view
- {
- return _isRefreshing;
- }
- - (NSDate *)egoRefreshTableDataSourceLastUpdated:(UIView *)view
- {
- return [NSDate date];
- }
- - (void)egoRefreshTableDidTriggerRefresh:(UIView*)view{
- [self reloadTableViewDataSource];
- double delayInSeconds = 1.5;
- dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
- dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
- [self performSelector:@selector(doneLoadingTableViewData)];
- });
- }
- - (void)reloadTableViewDataSource
- {
- _rowCount += 5;
- _isRefreshing = YES;
- }
- - (void)doneLoadingTableViewData
- {
- // model should call this when its done loading
- _isRefreshing = NO;
- [self.tableView reloadData];
- [_headerRefreshView egoRefreshScrollViewDataSourceDidFinishedLoading:self.tableView];
- //header
- _headerRefreshView.frame = CGRectMake(0, 0-self.tableView.bounds.size.height- 44, self.tableView.frame.size.width, self.tableView.bounds.size.height + 44);
- }
- - (void)scrollViewDidScroll:(UIScrollView *)scrollView
- {
- [_headerRefreshView egoRefreshScrollViewDidScroll:scrollView];
- }
- - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
- {
- [_headerRefreshView egoRefreshScrollViewDidEndDragging:scrollView];
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- if (tableView == self.searchDisplayController.searchResultsTableView)
- {
- return _searchList.count;
- }
- else
- {
- return _rowCount;
- }
- }
- - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
- {
- NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self CONTAINS %@", searchString];
- if (_searchList)
- {
- _searchList = nil;
- }
- _searchList = [NSMutableArray arrayWithArray:[_dataList filteredArrayUsingPredicate:predicate]];
- return YES;
- }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。