使用OC和swift创建系统自带的刷新界面

一:swift刷新界面代码:

import UIKit

class ViewController: UITableViewController {

// 用于显示的数据源
    var _dataSource:[String] = []
    
    // 加载更多 状态 风火轮
    var _aiv:UIActivityIndicatorView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        // 数据源中的基础数据
        for i in 0...2 {
            
            _dataSource.append("\(i)")
        }
        
        // 初始下拉刷新控件
        self.refreshControl = UIRefreshControl()
        self.refreshControl?.attributedTitle = NSAttributedString(string: "Pull To Refresh")
        self.refreshControl?.tintColor = UIColor.greenColor()
        self.refreshControl?.addTarget(self, action: "refresh", forControlEvents: .ValueChanged)
        
        // 加载更多按扭的背景视图
        var tableFooterView:UIView = UIView()
        tableFooterView.frame = CGRectMake(0, 0, self.view.bounds.size.width, 44)
        tableFooterView.backgroundColor = UIColor.greenColor()
        self.tableView.tableFooterView = tableFooterView

// 加载更多的按扭
        let loadMoreBtn = UIButton()
        loadMoreBtn.frame = CGRectMake(0, 0, self.view.bounds.width, 44)
        loadMoreBtn.setTitle("Load More", forState: .Normal)
        loadMoreBtn.setTitleColor(UIColor.lightGrayColor(), forState: .Normal)
        loadMoreBtn.addTarget(self, action: "loadMore:", forControlEvents: .TouchUpInside)
        tableFooterView.addSubview(loadMoreBtn)
        
        // 加载更多 状态 风火轮
        _aiv = UIActivityIndicatorView(activityIndicatorStyle: .Gray)
        _aiv.center = loadMoreBtn.center
        tableFooterView.addSubview(_aiv)
    }
    
    // 加载更多方法
    func loadMore(sender:UIButton) {
        
        sender.hidden = true
        _aiv.startAnimating()
        
        dispatch_async(dispatch_get_global_queue(0, 0), { () -> Void in
            
            self._dataSource.append("\(self._dataSource[self._dataSource.count-1].toInt()! + 1)")
            
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                
                sleep(1)
                
                self._aiv.stopAnimating()
                sender.hidden = false

self.tableView.reloadData()
            })
        })
    }
    
    // 下拉刷新方法
    func refresh() {
        
        if self.refreshControl?.refreshing == true {
            
            self.refreshControl?.attributedTitle = NSAttributedString(string: "Loading...")
        }
        
        dispatch_async(dispatch_get_global_queue(0, 0), { () -> Void in
            
            self._dataSource.insert("\(self._dataSource[0].toInt()! - 1)", atIndex: 0)
            
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                
                sleep(1)
                
                self.refreshControl?.endRefreshing()
                self.refreshControl?.attributedTitle = NSAttributedString(string: "Pull To Refresh")

self.tableView.reloadData()
            })
        })
    }
    
    // tableView dataSource
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return _dataSource.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let identifier = "cell"
        
        var cell = tableView .dequeueReusableCellWithIdentifier(identifier) as? UITableViewCell
        
        if cell == nil {
            
            cell = UITableViewCell(style: .Default, reuseIdentifier: identifier)
        }
        
        cell?.textLabel?.text = "\(_dataSource[indexPath.row])"
        
        return cell!
    }
}

二:OC刷新界面代码:

#import "ViewController.h"

@interface ViewController ()
{
    // 数据源
    NSMutableArray * _dataSource;
    
    // 风火轮
    UIActivityIndicatorView * _aiv;
}

@end

@implementation ViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    // 初始数据源
    _dataSource = [[NSMutableArray alloc] init];
    
    // 基础数据
    for (int i=0; i<3; i++) {
        
        [_dataSource addObject:[NSString stringWithFormat:@"%d",i]];
    }
    
    // 刷新控件
    self.refreshControl = [[UIRefreshControl alloc] init];
    self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull To Refresh"];
    self.refreshControl.tintColor = [UIColor greenColor];
    [self.refreshControl addTarget:self action:@selector(refresh) forControlEvents:UIControlEventValueChanged];

// 背景视图
    UIView * tableFooterView = [[UIView alloc] init];
    tableFooterView.frame = CGRectMake(0, 0, self.view.bounds.size.width, 44);
    tableFooterView.backgroundColor = [UIColor greenColor];
    self.tableView.tableFooterView = tableFooterView;
    
    // 加载更多按扭
    UIButton * loadMoreBtn = [[UIButton alloc] init];
    loadMoreBtn.frame = CGRectMake(0, 0, self.view.bounds.size.width, 44);
    [loadMoreBtn setTitle:@"Load More" forState:UIControlStateNormal];
    [loadMoreBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [loadMoreBtn addTarget:self action:@selector(loadMore:) forControlEvents:UIControlEventTouchUpInside];
    [tableFooterView addSubview:loadMoreBtn];

// 风火轮
    _aiv = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    _aiv.center = loadMoreBtn.center;
    [tableFooterView addSubview:_aiv];
}

// 加载更多方法
- (void)loadMore:(UIButton *)sender
{
    sender.hidden = YES;
    [_aiv startAnimating];
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
       
        [_dataSource addObject: [NSString stringWithFormat:@"%d", [_dataSource[_dataSource.count-1] intValue] + 1]];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            sleep(1);
            
            [_aiv stopAnimating];
            sender.hidden = NO;
            
            [self.tableView reloadData];
        });
    });
}

// 下拉刷新方法
- (void)refresh {
    
    if (self.refreshControl.refreshing) {
        
        self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Loading..."];
    }

dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
    [_dataSource insertObject:[NSString stringWithFormat:@"%d", [_dataSource[0] intValue] -1] atIndex:0];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            sleep(1);
            
            [self.refreshControl endRefreshing];
            self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull To Refresh"];
            
            [self.tableView reloadData];
        });
    });
}

// tableView dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return _dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString * identifier = @"cell";
    
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    cell.textLabel.text = _dataSource[indexPath.row];
    
    return cell;
}

@end

使用OC和swift创建系统自带的刷新界面的更多相关文章

  1. Android调用系统自带的设置界面

    Android有很多系统自带的设置界面,如设置声音,设置网络等. 在开发中可以调用这些系统自带的设置界面. 点击以下列表中的选项,就可以调出相应的系统自带的设置界面. 如点击“无线和网络设置”,可以调 ...

  2. OC与Swift创建pod

    Cocoa pods 是iOS最常用的类库管理工具   OC的使用   删除源   sudo gem sources -r https://rubygems.org/ 添加源(使用淘宝的镜像,记住要用 ...

  3. 在OC代码中创建Swift编写的视图控制器

    背景 近日在和一群朋友做项目,我和另一位同学负责iOS客户端,我是一直使用OC的,而他只会Swift,因此在我们分工协作之后,就需要把代码合在一起,这就牵扯到如何在TabbarController中添 ...

  4. [Swift通天遁地]二、表格表单-(4)使用系统自带的下拉刷新控件,制作表格的下拉刷新效果

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  5. 如何使用win7自带的备份还原以及创建系统镜像------傻瓜式教程

    对于经常鼓捣电脑的童鞋来说,装系统是一件极其平常的事情,不过系统装多了之后,我们会感到比较烦躁,因为每一次装系统意味着驱动的重新安装,程序的重新安装,每次这么鼓捣几次,半天时间就花在这上面了,效率是在 ...

  6. Win10使用自带功能创建系统映像备份时D盘被包含进去问题的解决

    在使用Windows10系统时,使用Windows自带功能创建系统映像备份文件时碰到了一些问题,所以在此记录一下. 创建系统映像文件的步骤,如下: 1.打开 控制面板 -> 选择 系统和安全 - ...

  7. iOS开发——运行时OC篇&使用运行时获取系统的属性:使用自己的手势修改系统自带的手势

    使用运行时获取系统的属性:使用自己的手势修改系统自带的手势 有的时候我需要实现一个功能,但是没有想到很好的方法或者想到了方法只是那个方法实现起来太麻烦,一或者确实为了装逼,我们就会想到iOS开发中最牛 ...

  8. iOS代码规范(OC和Swift)

    下面说下iOS的代码规范问题,如果大家觉得还不错,可以直接用到项目中,有不同意见 可以在下面讨论下. 相信很多人工作中最烦的就是代码不规范,命名不规范,曾经见过一个VC里有3个按钮被命名为button ...

  9. iOS OC和Swift进行互相调用

    有时候 ,我们会涉及到双向混合编程,特别是OC和swift的互相引用. swift调用oc的方法: 1.桥接文件,一般是swift工程,在创建一个oc文件时,系统自动添加(不用改名,直接默认即可) 2 ...

随机推荐

  1. Visual Studio 2015 Owin+MVC+WebAPI+ODataV4+EntityFrawork+Identity+Oauth2.0+AngularJS 1.x 学习笔记之"坑"

    1.AngularJS route 与 MVC route http://www.cnblogs.com/usea/p/4211989.html public class SingleRoute : ...

  2. LINUX下为LVM磁盘增加硬盘空间

    总结: ~~~~~~~~~~~~~~~~~~~~ fdisk -lpvcreate /dev/sdbvgextend VolGroup /dev/sdblvextend -L +180G /dev/m ...

  3. Linux系统编程(30)—— socket编程之TCP/IP协议

    在世界上各地,各种各样的电脑运行着各自不同的操作系统为大家服务,这些电脑在表达同一种信息的时候所使用的方法是千差万别.就好像圣经中上帝打乱了各地人的口音,让他们无法合作一样.计算机使用者意识到,计算机 ...

  4. 如何在Excel中启用宏?

    OFFICE2003版本中启用宏的方法: 1.首先打开EXCEL应用程序. 2.点击上方的"工具"--"宏"--"安全性" 3.在" ...

  5. 【2012天津区域赛】部分题解 hdu4431—4441

    1001: 题意:给你13张麻将牌,问可以胡哪些张 思路: 枚举可能接到的牌,然后dfs判断能否胡 1002: 题意: 已知n,m 求 n的所有约数在m进制下的平方和 做法:队长用java高精度写的 ...

  6. 关于Python中的for循环控制语句

    #第一个:求 50 - 100 之间的质数 import mathfor i in range(50, 100 + 1):    for j in range(2, int(math.sqrt(i)) ...

  7. git 错误

    1  执行  Git add  somefile 的时候,出现 如下 错误: If no other git process is currently running, this probably m ...

  8. 菜单栏始终浮动在顶部 js

    //菜单栏始终浮动在顶部var navH = $(".trade-tab-bot").offset().top;//获取要定位元素距离浏览器顶部的距离//滚动条事件$(window ...

  9. Face recognition using Histograms of Oriented Gradients

    Face recognition using Histograms of Oriented Gradients 这篇论文的主要内容是将Hog算子应用到人脸识别上. 转载请注明:http://blog. ...

  10. SD卡中FAT32文件格式高速入门(图文具体介绍)

    说明: MBR :Master Boot Record ( 主引导记录) DBR :DOS Boot Record ( 引导扇区) FAT :File Allocation Table ( 文件分配表 ...