ios中scrollview怎样下拉刷新
2016-06-14 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
简单的实现了下拉刷新效果
代码如下
#import "ViewController.h"
@interface ViewController (){
UITableView * _tableView;
NSMutableArray * _dataArray;
UIView * _loadView;
}
@end
@implementation ViewController
- (void)dealloc{
[_tableView release];
[_dataArray release];
[_loadView release];
[super dealloc];
}
- (void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_tableView = [[UITableView alloc] initWithFrame:self.view.frame];
_tableView.backgroundColor = [UIColor whiteColor];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
_loadView = [[UIView alloc] initWithFrame:CGRectMake(0, -44, 320, 44)];
_loadView.backgroundColor = [UIColor whiteColor];
UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(150, 10, 20, 10)];
button.backgroundColor = [UIColor blueColor];
[button addTarget:self action:@selector(btnCicked:)forControlEvents:UIControlEventTouchUpInside];
[_loadView addSubview:button];
[self.view addSubview:_loadView];
_dataArray = [NSMutableArray arrayWithObjects:@"1",@"2",@"3", nil];
}
- (IBAction)btnCicked:(id)sender{
//这里可以换做处理刷新完成之后的处理,比如insert data row 等
[UIView animateWithDuration:0.2 animations:^(void){
_tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
}completion:nil];
}
- (void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString * indentify = [NSString stringWithFormat:@"UITableViewCellIdentify"];
UITableViewCell * cell = [_tableViewdequeueReusableHeaderFooterViewWithIdentifier:indentify];
if (cell) {
for (UIView * mview in cell.subviews) {
[mview removeFromSuperview];
}
}
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:indentify];
}
cell.textLabel.text = [_dataArray objectAtIndex:indexPath.row];
UIImageView * view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
view.backgroundColor = [UIColor orangeColor];
[cell addSubview:view];
[view release];
return cell;
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{
return 44;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
_loadView.frame = CGRectMake(0, -44 - scrollView.contentOffset.y, 320, 44);
if (scrollView.contentOffset.y<= -44) {
//处理松开刷新提示
}else{
//关闭松开刷新提示
}
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
if (scrollView.contentOffset.y<= -44) {
//此处动画纯粹为了过度平滑
[UIView animateWithDuration:0.2 animations:^(void){
_tableView.contentInset = UIEdgeInsetsMake(44, 0, 0, 0);
}completion:^(BOOL complete){
//处理需要加载的东西
}];
}
}
@end
代码如下
#import "ViewController.h"
@interface ViewController (){
UITableView * _tableView;
NSMutableArray * _dataArray;
UIView * _loadView;
}
@end
@implementation ViewController
- (void)dealloc{
[_tableView release];
[_dataArray release];
[_loadView release];
[super dealloc];
}
- (void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_tableView = [[UITableView alloc] initWithFrame:self.view.frame];
_tableView.backgroundColor = [UIColor whiteColor];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
_loadView = [[UIView alloc] initWithFrame:CGRectMake(0, -44, 320, 44)];
_loadView.backgroundColor = [UIColor whiteColor];
UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(150, 10, 20, 10)];
button.backgroundColor = [UIColor blueColor];
[button addTarget:self action:@selector(btnCicked:)forControlEvents:UIControlEventTouchUpInside];
[_loadView addSubview:button];
[self.view addSubview:_loadView];
_dataArray = [NSMutableArray arrayWithObjects:@"1",@"2",@"3", nil];
}
- (IBAction)btnCicked:(id)sender{
//这里可以换做处理刷新完成之后的处理,比如insert data row 等
[UIView animateWithDuration:0.2 animations:^(void){
_tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
}completion:nil];
}
- (void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString * indentify = [NSString stringWithFormat:@"UITableViewCellIdentify"];
UITableViewCell * cell = [_tableViewdequeueReusableHeaderFooterViewWithIdentifier:indentify];
if (cell) {
for (UIView * mview in cell.subviews) {
[mview removeFromSuperview];
}
}
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:indentify];
}
cell.textLabel.text = [_dataArray objectAtIndex:indexPath.row];
UIImageView * view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
view.backgroundColor = [UIColor orangeColor];
[cell addSubview:view];
[view release];
return cell;
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{
return 44;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
_loadView.frame = CGRectMake(0, -44 - scrollView.contentOffset.y, 320, 44);
if (scrollView.contentOffset.y<= -44) {
//处理松开刷新提示
}else{
//关闭松开刷新提示
}
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
if (scrollView.contentOffset.y<= -44) {
//此处动画纯粹为了过度平滑
[UIView animateWithDuration:0.2 animations:^(void){
_tableView.contentInset = UIEdgeInsetsMake(44, 0, 0, 0);
}completion:^(BOOL complete){
//处理需要加载的东西
}];
}
}
@end
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询