iOS UITableView的下拉刷新

#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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值