这几天因为住的地方的网出了一点问题,除了能上Q,上微博以外其他的网页全都无法登陆。博客也就没有跟进。
今天恢复了,所以继续更新博客。也希望大家能继续评论或私自给我一些建议或者交流:-)
今天找到了以前一个TableView的例子,主要关于上下拉刷新的,所以写了一个demo,然后更新到博客上来。
/******内置刷新的常用属性设置******/ 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; }