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文件中添加:
AppDelegate.m文件中修改:
ViewController.h文件:
ViewController.m文件:
在 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; }
相关文章推荐
- ueditor使用会于第三方发生冲突
- UIScrollView中contentSize、contentInset、contentOffset介绍
- 解决The current branch is not configured for pull No value for key branch.master.merge found in confi
- CoreBluetooth框架(转)
- UILabel
- String,StringBuffer与StringBuilder的区别
- iOS 教你学UI控件的封装
- PHP 性能分析第一篇: Intro to Xhprof & Xhgui
- 法院执行人信息 业务 使用了多线程 cookie BlockingQueue 的一本程序
- Rails 多态关联 :polymorphic => true
- proguardgui.bat来混淆已有的jar包
- 如何在 Ubuntu 中安装最新的 Arduino IDE 1.6.6
- 自定义Label文字绘制区域
- NGUI
- requireJS的使用
- 改变 UITextField 提示语的 字体颜色
- MySQL启动错误Starting MySQL.Manager of pid-file quit without
- 使用Grub Rescue 修复MBR
- Sqoop User Guide (v1.4.6)
- 使用Grub Rescue 修复MBR