• 简单实现下拉刷新数据


     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

    刷新前                                                 刷新后

                

  • 相关阅读:
    基于 Spring Cloud 的微服务架构实践指南(上)
    1分钟,带你上手“Markdown”语法
    Spring Cloud 理论篇
    如何设计一个优雅的RESTFUL的接口
    简简单单之Linux命令入门
    SpringBoot基础架构篇3(Redis)
    SpringBoot基础架构篇1(SpringBoot、MyBatis-Plus与Thymeleaf)
    基础之Lambda和Stream的邂逅
    浏览器如何执行JS
    渲染进程里的线程
  • 原文地址:https://www.cnblogs.com/lantu1989/p/4604679.html
Copyright © 2020-2023  润新知