• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

一: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

 

一: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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap