iOS UITableView的下拉刷新

来源:互联网 发布:如何查看本机mac地址 编辑:程序博客网 时间:2024/06/05 19:54
#import "ViewController.h"@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>@end@implementation ViewController {    UIRefreshControl *_refresh;    UITableView *_tableView;    NSMutableArray *_dataArray;}-(BOOL)prefersStatusBarHidden {    return YES;}- (void)viewDidLoad {    [super viewDidLoad];    _dataArray=[[NSMutableArray alloc]init];    for (int i=0; i<6; i++) {        NSString *string=[NSString stringWithFormat:@"第%d行数据",i];        [_dataArray addObject:string];    }    _tableView=[[UITableView alloc]initWithFrame:self.view.frame 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(refreshAction) forControlEvents:UIControlEventValueChanged];    [_tableView addSubview:_refresh];    }static int newDataCount = 0;//记录下拉加载数据的条数- (void)refreshAction//No.1//开始写代码,实现下拉_tableView加载出一条新的数据。注意刷新_tableView和停止刷新。{ [_dataArray  insertObject:@"refresh"  atIndex:0]; [_tableView reloadData];//UITableView重新加载数据 [_refresh endRefreshing];//停止刷新}//end_code- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {        return _dataArray.count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {        UITableViewCell * cell = [_tableView dequeueReusableCellWithIdentifier:@"cell"];    if (cell == nil) {        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];    }    cell.textLabel.text = [_dataArray objectAtIndex:indexPath.row];        return cell;}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];}@end


0 0