概述:
Refresh control 是ios6之后新添加的刷新控件,它的使用非常简单,出现在表格的顶部,当表格出现下拉的时候,出现动画效果告诉用户,表格正在加载。像现在的微博,QQ,twitter的客户端,无处不见这种加载效果。
代码:
要使用Refresh control,我们要实现它的addTarget:action:forControlEvents: 实例方法,在今天这个demo里面,我们需要一个UItableView
@property (nonatomic, strong) NSMutableArray *times; @property (nonatomic, strong) UIRefreshControl *refreshControl;
初始化表格的时候,重载的方法:
- (id)initWithStyle:(UITableViewStyle)style{ self = [super initWithStyle:style]; if (self) { self.times = [NSMutableArray arrayWithObject:[NSDate date]]; /* Create the refresh control */ self.refreshControl = [[UIRefreshControl alloc] init]; self.refreshControl = self.refreshControl; [self.refreshControl addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventValueChanged]; } return self; }
添加表格数据和添加refresh control 的target:
- (void) handleRefresh:(id)paramSender{ /* Put a bit of delay between when the refresh control is released and when we actually do the refreshing to make the UI look a bit smoother than just doing the update without the animation */ int64_t delayInSeconds = 1.0f; dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ /* Add the current date to the list of dates that we have so that when the table view is refreshed, a new item will appear on the screen so that the user will see the difference between the before and the after of the refresh */ [self.times addObject:[NSDate date]]; [self.refreshControl endRefreshing]; [self.tableView reloadData]; }); }
表格的数据源的方法:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.times.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } cell.textLabel.text = [NSString stringWithFormat:@"%@", self.times[indexPath.row]]; return cell; }
实现效果如下:
相关推荐
iOS自定义下拉刷新控件
如果想不用三方自己写一个刷新控件可以参考源码 文件太大了,下面是GitHub地址,喜欢的给个star支持 https://github.com/aizexin/AIAnimationDemo 简书ID:艾江山 http://www.jianshu.com/p/511733d5c3b6 ...
Refresh 刷新 IOS 下拉 上拉.zip
IOS上拉下拉刷新控件,有事例参考,绝对可用,接口明确,使用方便
非常好用的 刷新控件 继承上拉刷新 下拉加载 几行代码搞定 可以自定义刷新时候的文字
仿"携程"的下拉刷新控件,在此基础上又增加了6种可选类型,同时集成了上拉刷新.代码已经封装好,几句代码就可以使用起来,快速方便,在此把源码放出来,提供给有需要的人或学习交流. github地址:...
见过的android中仿iphone最好的控件,实现下拉刷新 上提加载等功能
现在iOS里有很多成熟的下拉刷新控件,比如MJRefresh,SVPullToRefresh 我这里参考了SV的写法,但是回调用的是代理,没有用block,个人感觉用代理更简洁一点 下拉刷新的基本原理 在scrollview的上面和下面分别添加一...
高仿iOS原生风格日历控件.zip,高仿iOS原生风格日历控件
下拉刷新控件,使用简单.支持多种动画类型.支持继承UIScrollview的控件(UITableview,UICollectionview)
iOS淘宝商品详情控件
参考MJRefresh,写了Swift版本的下拉刷新功能
Android 高仿 IOS日历 滚轮选择控件 Android 高仿 IOS日历 滚轮选择控件
ios应用源码之控件的使用 .
ios列表下拉刷新
使用EGOTableViewPullRefresh开源项目在IOS中实现UITableView的下拉刷新功能,对源码进行了一些改动,支持中英文显示,刷新时间格式也做了修改
Android通用下拉刷新控件的使用
ios下拉框控件
iOS 视频播放控件 DZVideoPlayerViewController ,DZVideoPlayerViewController 是一款 iOS 的视频播放器控件,有播放控制...