APPDelegate.m
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.rootViewController = [[UINavigationController alloc]initWithRootViewController:[[MyTableViewController alloc]init]];
[self.window makeKeyAndVisible];
return YES;
}
.M
#import "MyTableViewController.h"
-(NSMutableArray *)allDatas {
if (_allDatas == nil) {
_allDatas = [@[@"张三", @"李四", @"王五"] mutableCopy];
}
return _allDatas;
}
- (void)viewDidLoad {
[super viewDidLoad];
//1.创建UIRefreshContol控件实例
UIRefreshControl *rc = [[UIRefreshControl alloc]init];
//2.将控件实例赋值给表格控制器的refreshControl属性
self.refreshControl = rc;
//3.添加 valueChanged事件的监听
[rc addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
}
-(void)refresh:(UIRefreshControl*)sender {
NSLog(@"加载数据");
//模拟加载数据时间2秒钟
//模拟从网上 下来了 一个数据 赵六
NSString *zhaoliu = @"赵六";
[self performSelector:@selector(refreshOver:) withObject:zhaoliu afterDelay:2];
}
-(void)refreshOver:(NSString *)str {
//将数据添加到 数组中
[self.allDatas addObject:str];
//刷新 tableView
[self.tableView reloadData];
//停止刷新
[self.refreshControl endRefreshing];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.allDatas.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"id"];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"id"];
}
cell.textLabel.text = self.allDatas[indexPath.row];
return cell;
}