UITableView下拉刷新

来源:互联网 发布:美国圣劳伦斯学院知乎 编辑:程序博客网 时间:2024/06/09 18:31

//  ViewController.h

//  ReferebcceDemo

//

//  Created by lengshengren on 13-9-10.

//  Copyright (c) 2013 lengshengren. All rights reserved.

//


#import <UIKit/UIKit.h>

@interface ViewController :UITableViewController <UITableViewDataSource,UITableViewDelegate>

{

    NSMutableArray *timeArray;

    UIRefreshControl *refresh;

}

@property (strong,nonatomic)NSMutableArray *timeArray;

@property (strong,nonatomic)UIRefreshControl *refresh;

@property (strong, nonatomic) IBOutlet UITableView *tableview;


@end



//  ViewController.m

//  ReferebcceDemo

//

//  Created by lengshengren on 13-9-10.

//  Copyright (c) 2013 lengshengren. All rights reserved.

//

#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController

@synthesize refresh;

@synthesize timeArray;

@synthesize tableview;

- (void)viewDidLoad

{

    [superviewDidLoad];

    

   //UIRefreshControl目前只能用于UITableViewController,我写时后用的是viewCtntrol 发现调用不了 self.refreshControl属性

    

    self.timeArray = [[NSMutableArrayalloc]init];

    [selfsetbeginRefreshing];

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

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

   

    return 1;

}


#pragma 开始刷新函数

- (void)setbeginRefreshing

{

    refresh = [[UIRefreshControlalloc]init];

    //刷新图形颜色

    refresh.tintColor = [UIColorlightGrayColor]; 

    //刷新的标题

    refresh.attributedTitle = [[NSAttributedStringalloc]initWithString:@"下拉刷新"];

   

  // UIRefreshControl 会触发一个UIControlEventValueChanged事件,通过监听这个事件,我们就可以进行类似数据请求的操作了

    [refreshaddTargetselfaction:@selector(refreshTableviewAction:)forControlEvents:UIControlEventValueChanged];

    self.refreshControl =refresh;

    

}


-(void)refreshTableviewAction:(UIRefreshControl *)refreshs

{

    

    

    if (refreshs.refreshing) {

        refreshs.attributedTitle = [[NSAttributedStringalloc]initWithString:@"正在刷新"];

        [selfperformSelector:@selector(refershData)withObject:nilafterDelay:2];

    }


   

}


-(void)refershData

{


    NSString *syseTiem = nil;

    NSDateFormatter *formatter = [[NSDateFormatteralloc]init];

    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//创建的时间格式

    syseTiem = [formatter stringFromDate:[NSDatedate]];

    

    NSString *lastUpdated = [NSStringstringWithFormat:@"上一次更新时间为 %@", [formatter stringFromDate:[NSDate date]]];

    

     self.refreshControl.attributedTitle = [[NSAttributedStringalloc]initWithString:lastUpdated] ;

    

    [self.timeArrayaddObject:syseTiem];

    [self.refreshControlendRefreshing];

    [self.tableviewreloadData];



}

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

{

    return self.timeArray.count;

}


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

{

    static NSString *CellIdentifier =@"Cell";

    UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier];

    

   if (cell == nil) {

        cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier] ;

    }

    

    cell.textLabel.text = [self.timeArrayobjectAtIndex:indexPath.row];

   

    return cell;

}


- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end





0 0