• 使用系统自带的 UIRefreshControl 实现下拉刷新


    UIRefreshControl 为 UITableViewController 中的一个属性,从以下可以看出, IOS6.0 以上才支持.

    @property (nonatomic,retain) UIRefreshControl *refreshControl NS_AVAILABLE_IOS(6_0);


    RootViewController.h

    //  Copyright (c) 2014年 YouXian. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    
    @interface RootViewController : UITableViewController
    
    @end

    RootViewController.m

    //  Copyright (c) 2014年 YouXian. All rights reserved.
    //
    
    #import "RootViewController.h"
    
    @interface RootViewController ()
    
    @property (nonatomic) NSMutableArray *data;
    
    @end
    
    @implementation RootViewController
    
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
        //初始化数据源
        _data = [[NSMutableArray alloc] init];
    
        //初始化refreshControl
        self.refreshControl = [[UIRefreshControl alloc] init];
        self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"下拉刷新"];
        [self.refreshControl addTarget:self
                                action:@selector(RefreshViewControlEventValueChanged)
                      forControlEvents:UIControlEventValueChanged];
    }
    
    - (void)RefreshViewControlEventValueChanged
    {
        [self performSelector:@selector(getDataAndStopRefresh)
                   withObject:nil
                   afterDelay:2];
    }
    
    - (void)getDataAndStopRefresh
    {
        //处理数据
        for (int i = 0; i <= arc4random() % 4; i++)
        {
            [_data addObject:[NSString stringWithFormat:@"%@", [NSDate date].description]];
        }
    
        //结束刷新(重要,只有先处理完了数据源,再执行方法 endRefreshing 让tableView回去)
        [self.refreshControl endRefreshing];
        
        //重新加载
        [self.tableView reloadData];
    }
    
    #pragma mark - Table view data source
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return _data.count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                          reuseIdentifier:CellIdentifier];
        }
        
        cell.textLabel.text = [NSString stringWithFormat:@"%@", _data[indexPath.row]];
        
        return cell;
    }
    
    @end

    在下拉刷新后,只有执行了方法 [self.refreshControl endRefreshing]; 才能够恢复,简单易用,不过需要在执行这个方法之前处理好一切的数据.

  • 相关阅读:
    第2章 ActionScript教程 显示
    第0章 ActionScript教程 Flash相关概念
    PHP中可速查的知识点
    ActionScript3中的Int64与ByteArray转换
    第9章 ActionScript教程 应用程序国际化
    关于一个经典的委托和事件的问题 关于老鼠 猫 主人问题 帝都
    页面导入样式时,使用link和@import有什么区别
    圣杯布局和双飞翼布局的理解和区别,并用代码实现
    Mac电脑 python3.9 连接SQL Server报错
    MAC电脑如何将常规视频中音频提取出来(转换格式并调整采样频率),并利用python结合讯飞语音识别文字
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/3584113.html
Copyright © 2020-2023  润新知