zoukankan      html  css  js  c++  java
  • 使用开源库 SVPullToRefresh 实现上拉加载下拉刷新

    SVPullToRefresh开源库地址

    https://github.com/samvermette/SVPullToRefresh

    将整个文件夹SVPullToRefresh拖入工程中并引入头文件即可

    注意编译时有一个方法快被弃用了

    - (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(NSLineBreakMode)lineBreakMode

    工程源码

    RootViewController.h

    1 //  Copyright (c) 2014年 YouXian. All rights reserved.
    2 //
    3 
    4 #import <UIKit/UIKit.h>
    5 
    6 @interface RootViewController : UIViewController
    7 
    8 @end

    RootViewController.m

      1 //  Copyright (c) 2014年 YouXian. All rights reserved.
      2 //
      3 
      4 #import "RootViewController.h"
      5 #import "SVPullToRefresh.h"
      6 
      7 @interface RootViewController () <UITableViewDelegate, UITableViewDataSource>
      8 
      9 @property (nonatomic, strong) UITableView     *tableView;
     10 @property (nonatomic, strong) NSMutableArray  *dataSource;
     11 
     12 @end
     13 
     14 @implementation RootViewController
     15 
     16 - (void)viewDidLoad
     17 {
     18     [super viewDidLoad];
     19     
     20     //初始化 tableView
     21     _tableView = [[UITableView alloc] initWithFrame:self.view.bounds
     22                                               style:UITableViewStyleGrouped];
     23     _tableView.delegate   = self;
     24     _tableView.dataSource = self;
     25     [self.view addSubview:_tableView];
     26     
     27     //初始化数据源
     28     _dataSource = [[NSMutableArray alloc] init];
     29     for (int i = 0; i < 10; i++)
     30     {
     31         [_dataSource addObject:[NSString stringWithFormat:@"%@", [NSDate date].description]];
     32     }
     33     
     34     //注册下拉刷新功能
     35     __weak RootViewController *weakSelf = self;
     36     [_tableView addPullToRefreshWithActionHandler:^{
     37         [weakSelf insertRowAtTop];
     38     }];
     39     
     40     //注册上拉刷新功能
     41     [_tableView addInfiniteScrollingWithActionHandler:^{
     42         [weakSelf insertRowAtBottom];
     43     }];
     44 }
     45 
     46 #pragma mark -
     47 #pragma mark PullToRefreshInsertRow
     48 
     49 - (void)insertRowAtTop
     50 {
     51     int64_t delayInSeconds = 2.0;
     52     dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
     53     dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
     54         //开始更新
     55         [_tableView beginUpdates];
     56 
     57         //插入数据到数据源(数组的开头)
     58         [_dataSource insertObject:[NSString stringWithFormat:@"%@", [NSDate date].description]
     59                                                      atIndex:0];
     60         
     61         //在tableView中插入一行(Row开头)
     62         [_tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0
     63                                                                 inSection:0]]
     64                           withRowAnimation:UITableViewRowAnimationBottom];
     65         
     66         //结束更新
     67         [_tableView endUpdates];
     68         
     69         //停止菊花
     70         [_tableView.pullToRefreshView stopAnimating];
     71     });
     72 }
     73 
     74 - (void)insertRowAtBottom
     75 {
     76     int64_t delayInSeconds = 2.0;
     77     dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
     78     dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
     79         //开始更新
     80         [_tableView beginUpdates];
     81 
     82         //插入数据到数据源(数组的结尾)
     83         [_dataSource addObject:[NSString stringWithFormat:@"%@", [NSDate date].description]];
     84         
     85         
     86         //在tableView中插入一行(Row结尾)
     87         [_tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:_dataSource.count - 1
     88                                                                 inSection:0]]
     89                           withRowAnimation:UITableViewRowAnimationBottom];
     90         
     91         //结束更新
     92         [_tableView endUpdates];
     93         
     94         //停止菊花
     95         [_tableView.infiniteScrollingView stopAnimating];
     96     });
     97 }
     98 
     99 #pragma mark -
    100 #pragma mark UITableViewDataSource
    101 
    102 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    103 {
    104     return 1;
    105 }
    106 
    107 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    108 {
    109     return _dataSource.count;
    110 }
    111 
    112 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    113     static NSString *identifier = @"Cell";
    114     UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:identifier];
    115     
    116     if (cell == nil)
    117     {
    118         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
    119                                       reuseIdentifier:identifier];
    120     }
    121     
    122     cell.textLabel.text = _dataSource[indexPath.row];
    123     
    124     return cell;
    125 }
    126 
    127 @end

    心得:

    使用简单,逻辑清晰,开源库使用block实现, RootViewController.m 35行代码处要将RootViewController自身传入block中,需要使用弱应用指针,注意.

    工程源码地址:

    http://pan.baidu.com/s/1dD24E1V

  • 相关阅读:
    【FPGA篇章四】FPGA状态机:三段式以及书写方法
    【FPGA篇章三】FPGA常用语句:Verilog基本语法要素
    【FPGA篇章二】FPGA开发流程:详述每一环节的物理含义和实现目标
    【FPGA篇章一】FPGA工作原理:详细介绍FPGA实现编程逻辑的机理
    学习python随笔记
    Spring中的@Bean注解、@Configuration注解、@Value
    什么是Maven项目
    SpringBoot(四)thymeleaf+MyBatis+MySql
    SpringBoot(三)thymeleaf+JPA+MySql
    SpringBoot(二)thymeleaf模板的引入
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/3584342.html
Copyright © 2011-2022 走看看