#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
iOS UITableView的下拉刷新
最新推荐文章于 2020-09-26 22:43:29 发布