首先准备数据数组
-(NSMutableArray *)array {
if (!_array) {
_array = [@[@100,@100,@100,@100,@100,@100,@100,@100,@100] mutableCopy];
}
return _array;
}
viewdidload中定义下拉刷新的空间。并且实现下拉刷新触发的方法。
- (void)viewDidLoad {
[super viewDidLoad];
//添加下拉刷新
UIRefreshControl *refreshControl = [[UIRefreshControl alloc]init];
//用创建好的对象 给 tableViewController 的 refreshControl 赋值
self.refreshControl = refreshControl;
//设置 刷新触发的事件方法
[refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
}
-(void)refresh:(UIRefreshControl*)sender {
//加载数据
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.array insertObject:@200 atIndex:0];
[self.tableView reloadData];
//关闭下拉刷新
[self.refreshControl endRefreshing];
});
}
实现tableview代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier"];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuseIdentifier"];
}
cell.textLabel.text = [self.array[indexPath.row] stringValue];
return cell;
}