使用OC和swift建立系統內建的重新整理介面

來源:互聯網
上載者:User

標籤:

使用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建立系統內建的重新整理介面

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.