欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS表视图之下拉刷新控件功能的实现方法

程序员文章站 2024-02-17 20:26:16
下拉刷新是重新刷新表视图或列表,以便重新加载数据,这种模式广泛用于移动平台,相信大家对于此也是非常熟悉的,那么ios是如何做到的下拉刷新呢? 在ios 6之后,uitab...

下拉刷新是重新刷新表视图或列表,以便重新加载数据,这种模式广泛用于移动平台,相信大家对于此也是非常熟悉的,那么ios是如何做到的下拉刷新呢?

在ios 6之后,uitableviewcontrol添加了一个refreshcontrol属性,该属性保持了uirefreshcontrol的一个对象指针。uirefreshcontrol就是表视图实现下拉刷新提供的类,目前该类只能用于表视图界面。下面我们就来试试该控件的使用。

编写代码之前的操作类似于前面几篇文章。代码如下:

#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表视图之下拉刷新控件功能的实现方法

iOS表视图之下拉刷新控件功能的实现方法

iOS表视图之下拉刷新控件功能的实现方法

以上所述是小编给大家介绍的ios表视图之下拉刷新控件功能的实现方法,希望对大家有所帮助