ios中scrollview怎样下拉刷新
答案:1 悬赏:40 手机版
解决时间 2021-02-20 11:07
- 提问者网友:謫仙
- 2021-02-19 13:44
ios中scrollview怎样下拉刷新
最佳答案
- 五星知识达人网友:北方的南先生
- 2021-02-19 15:22
简单的实现了下拉刷新效果
代码如下
#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
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯