码迷,mamicode.com
首页 > 移动开发 > 详细

iOS开发——OC和swift创建系统自带的刷新界面

时间:2015-05-27 09:53:56      阅读:2834      评论:0      收藏:0      [点我收藏+]

标签:

使用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

 
 

iOS开发——OC和swift创建系统自带的刷新界面

标签:

原文地址:http://www.cnblogs.com/iCocos/p/4532548.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!