代码改变世界

简单实现下拉刷新数据

2015-06-27 21:07  甘雨路  阅读(479)  评论(0编辑  收藏  举报
 1 #import "AppDelegate.h"
 2 #import "SearchController.h"
 3 @interface AppDelegate ()<UITableViewDataSource>
 4 @property (nonatomic,strong)UITableView *table;
 5 @property (nonatomic,strong)NSMutableArray *datas;
 6 @property (nonatomic,strong)UIRefreshControl *refreshControl;
 7 @end
 8 
 9 @implementation AppDelegate
10 
11 - (void)dealloc
12 {
13     self.datas = nil;
14     self.table = nil;
15     self.refreshControl = nil;
16 }
17 
18 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
19     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
20     self.window.backgroundColor = [UIColor whiteColor];
21 
22 
23     [self setupViews];
24     [self setupWithArray];
25     
26     [self.window makeKeyAndVisible];
27     return YES;
28     
29 }
30 /**
31  *  初始化数组
32  */
33 - (void)setupWithArray
34 {
35     NSArray *array = @[@"A",@"B",@"C",@"D",@"E",@"F"];
36     self.datas = [array mutableCopy];
37 }
38 /**
39  *  添加控件
40  */
41 - (void)setupViews
42 {
43     self.table = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
44     self.table.dataSource = self;
45     [self.window addSubview:self.table];
46     
47     self.refreshControl = [[UIRefreshControl alloc] init];
48     [self.refreshControl addTarget:self action:@selector(refreshControlChange:) forControlEvents:UIControlEventValueChanged];
49     [self.table addSubview:self.refreshControl];
50 
51 }
52 
53 #pragma mark - 配置数据源
54 /**
55  *  @return 返回显示行数
56  */
57 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
58 {
59     return self.datas.count;
60 }
61 /**
62  *  显示内容
63  */
64 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
65 {
66     static NSString *identify = @"cell";
67     UITableViewCell *cell = [self.table dequeueReusableCellWithIdentifier:identify];
68     if (cell == nil) {
69         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
70     }
71     cell.textLabel.text = self.datas[indexPath.row];
72     return cell;
73 }
74 /**
75  *  每次下拉刷新,就添加新数据
76  */
77 - (void)refreshControlChange:(UIRefreshControl *)sender
78 {
79     static int count = 1;
80     NSString *str = [NSString stringWithFormat:@"第%d次加数据",count];
81     NSArray *array = @[str];
82     NSMutableArray *newArray = [array mutableCopy];
83     [newArray addObjectsFromArray:self.datas];
84     self.datas = newArray;
85     count++;
86     /**  UITableView重新加载  */
87     [self.table reloadData];
88     /**   结束刷新 */
89     [self.refreshControl endRefreshing];
90 }
91 @end

刷新前                                                 刷新后