下拉刷新是重新刷新表视图或列表,以便重新加载数据,这种模式广泛用于移动平台,相信大家对于此也是非常熟悉的,那么ios是如何做到的下拉刷新呢?
在ios 6之后,uitableviewcontrol添加了一个refreshcontrol属性,该属性保持了uirefreshcontrol的一个对象指针。uirefreshcontrol就是表视图实现下拉刷新提供的类,目前该类只能用于表视图界面。下面我们就来试试该控件的使用。
编写代码之前的操作类似于前面几篇文章。代码如下:
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
#import "viewcontroller.h"
@interface viewcontroller ()
@end
@implementation viewcontroller
- (void)viewdidload {
[super viewdidload];
self.logs = [[nsmutablearray alloc]init];//初始化数据
nsdate * date = [[nsdate alloc]init];//初始化日期
[self.logs addobject:date];//把日期插入数据中
uirefreshcontrol * rc = [[uirefreshcontrol alloc]init];//初始化uirefreshcontrol
rc.attributedtitle = [[nsattributedstring alloc]initwithstring:@"下拉刷新"];//设置下拉框控件标签
[rc addtarget:self action:@selector(refreshaction) forcontrolevents:uicontroleventvaluechanged];//添加下拉刷新事件
self.refreshcontrol = rc;
// do any additional setup after loading the view, typically from a nib.
}
- (void)didreceivememorywarning {
[super didreceivememorywarning];
// dispose of any resources that can be recreated.
}
//下拉刷新事件
-(void)refreshaction
{
if(self.refreshcontrol.refreshing)
{
self.refreshcontrol.attributedtitle = [[nsattributedstring alloc]initwithstring:@"加载中"];//设置下拉框控件标签
nsdate * date = [[nsdate alloc]init];
[self.logs addobject:date];//每次刷新添加当前日期
[self.refreshcontrol endrefreshing];//结束刷新
self.refreshcontrol.attributedtitle = [[nsattributedstring alloc]initwithstring:@"下拉刷新"];
[self.tableview reloaddata];
}
}
#pragma mark
-(nsinteger)numberofsectionsintableview:(uitableview *)tableview
{
return 1;
}
-(nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section
{
return [self.logs count];
}
-(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath
{
uitableviewcell * cell = [tableview dequeuereusablecellwithidentifier:@"cell"];
nsdateformatter * dateformat =[[nsdateformatter alloc]init];//nsdate的转换类,可将nsdate转换为其它格式,或者转换为nsdate格式
[dateformat setdateformat:@"yyyy-mm-dd hh:mm:ss zzz"];//设定时间格式
cell.textlabel.text = [dateformat stringfromdate:[self.logs objectatindex:indexpath.row]];
cell.accessorytype = uitableviewcellaccessorydisclosureindicator;
return cell;
}
@end
|
效果:



以上所述是小编给大家介绍的ios表视图之下拉刷新控件功能的实现方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:http://blog.csdn.net/q5512049/article/details/54377094








发表评论
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。