UIRefreshControl模拟 刷新



<UITableViewDataSource,UITableViewDelegate>

代理

全局变量

{

    UITableView * _tableView;

    

    NSMutableArray * dataArray;

    

    UIRefreshControl * _refresh; //刷新

    

}

 

//加载数据

-(void)addData

{

    for (int i=0; i<10; i++)

    {

        [dataArray addObject:[NSString stringWithFormat:@"%d",arc4random()%100]];      

        

    }

 

 

}

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    //初始化数据源

    dataArray=[[NSMutableArray alloc]init];

    

    

    [self addData];

    

    //创建表格式图

    _tableView=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];

    

    _tableView.dataSource=self;

    

    _tableView.delegate=self;

    

    [self.view addSubview:_tableView];

    

    

    //添加刷新控件

    _refresh=[[UIRefreshControl alloc]init];

    

    _refresh.attributedTitle=[[NSAttributedString alloc]initWithString:@"正在刷新....."];

    

    //刷新控件 添加事件

    [_refresh addTarget:self action:@selector(refresh) forControlEvents:UIControlEventValueChanged];

    

    

    [_tableView addSubview:_refresh];

    

    

    

    

    

    

}

 

-(void)refresh

{

    //模拟网络延时这个过程  过多久时间去执行某个方法 延迟方法

    [self performSelector:@selector(refreshing) withObject:nil afterDelay:2];

 

 

}

-(void)refreshing

{

    [self addData];

    

    //回到主线程 执行结束的刷新方法

    [self performSelectorOnMainThread:@selector(finishedRefresh) withObject:nil waitUntilDone:YES];

    

 

 

}

-(void)finishedRefresh

{

    //结束刷新

    [_refresh endRefreshing];

    

    //刷新数据

    [_tableView reloadData];

    

    NSDate * data=[NSDate date];

    

    NSDateFormatter * matter=[[NSDateFormatter alloc]init];

    

    [matter setDateFormat:@"yyyy:MM:dd hh:mm:ss"];

    //将年月日时分秒

    NSString * currentDate=[matter stringFromDate:data];

    

    //设置刷新之后的提示语

    _refresh.attributedTitle=[[NSAttributedString alloc]initWithString:currentDate];

    

    

 

}

#pragma mark -----协议方法-----

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return [dataArray count];

 

}

 

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString * cellId=@"cellId";

    

    UITableViewCell * cell=[_tableView dequeueReusableCellWithIdentifier:cellId];

    

    if (cell ==nil) {

        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];

        

    }

    cell.textLabel.text=dataArray[indexPath.row];

    

    return cell;

 

 

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值