您的位置:首页 > 产品设计 > UI/UE

UITableViewController和UIRefreshControl实现下拉刷新功能

2015-12-24 11:25 453 查看
随着下拉刷新模式的影响力越来越大,苹果不得不考虑把它列入自己的规范之中,并在iOS 6 API中推出了下拉刷新控件.

在 iOS 6 之 后 , UITableViewController 添 加 了 一 个 refreshControl 属 性 , 这 个 属 性 保 持 了UIRefreshControl的一个对象指针。 UIRefreshControl就是iOS 6为表视图实现下拉刷新而提供的类,目前该类只能应用于表视图界面。

UIRefreshControl的refreshControl属性与UITableViewController配合使用,关于下拉刷新布局等问题可以不必考虑, UITableViewController会将其自动放置于表视图中。

下面用过代码(使用xib文件)介绍如何使用刷新控件,下拉刷新的时候会更新日期数据,并重新加载到表视图中,效果图如下:

图1:



图2



图3



AppDelegate.h文件中添加:

#import "ViewController.h"


AppDelegate.m文件中修改:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

self.window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
ViewController *myVc=[[ViewController alloc]init];
self.window.rootViewController=myVc;
[self.window makeKeyAndVisible];
return YES;

}


ViewController.h文件:

#import <UIKit/UIKit.h>

@interface ViewController : UITableViewController

@property(nonatomic,strong) UITableView *tabview;
@property(nonatomic,strong) NSMutableArray *nsMdate;
//nsMdate用于存放date数据
@property(nonatomic,strong) UIRefreshControl *refresh;

-(void) refreshTableview;
-(void) callBackMethod:(NSDate *) date;
@end


ViewController.m文件:

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSDate *date=[[NSDate alloc]init];
self.nsMdate=[[NSMutableArray alloc]init];
[self.nsMdate addObject:date];

self.tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain];
self.refresh=[[UIRefreshControl alloc]init];
self.refresh.attributedTitle=[[NSAttributedString alloc]initWithString:@"pull-down refresh"];
[self.refresh addTarget:self action:@selector(refreshTableview) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:self.refresh];

}
-(void) refreshTableview {
if (self.refresh.refreshing) {
self.refresh.attributedTitle=[[NSAttributedString alloc]initWithString:@"refreshing"];
NSDate *date=[[NSDate alloc]init];
[self performSelector:@selector(callBackMethod:) withObject:date afterDelay:1];
}
}
-(void) callBackMethod:(NSDate *)date{
[self.refresh endRefreshing];
self.refresh.attributedTitle=[[NSAttributedString alloc]initWithString:@"pull-down refresh"];
[self.nsMdate addObject:date];
[self.tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.nsMdate count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *ident=@"reuseid";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ident];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ident];
}
NSUInteger row=[indexPath row];
NSDateFormatter *dateform=[[NSDateFormatter alloc]init];
[dateform setDateFormat:@"yyyy-MM-dd : HH:mm:ss zzz"];
cell.textLabel.text=[dateform stringFromDate:[self.nsMdate objectAtIndex:row]];
return cell;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: