UIRefreshControl 刷新

首先,在viewDidLoad中初始化相关数据:

    override func viewDidLoad() {
       
super.viewDidLoad()
       
// Do any additional setup after loading the view.
       
       
//添加刷新
        refreshControl.addTarget(
self, action:"refreshData", forControlEvents: UIControlEvents.ValueChanged)
        refreshControl.attributedTitle = NSAttributedString(string:
"松手刷新新闻")
        newsTableView.addSubview(refreshControl)
       
    }
   
   这样下拉刷新已经可以看到了:
      
    <"http://www.2cto.com/kf/ware/vc/"target="_blank"class="keylink">vcD48cD6908/CwLSw0VNlbGVjdG9yIM3qycbSu8/Co6zM7bzTcmVmcmVzaERhdGG3vbeoo7o8L3A+PHA+PHByZSBjbGFzcz0="brush:java;">// 刷新数据 func refreshData() { let bquery = BmobQuery(className: "News") bquery.findObjectsInBackgroundWithBlock({array, error in self.dataArray = array self.newsTableView.reloadData() self.refreshControl.endRefreshing() }) }
   
   
   
完整的代码如下,因为涉及到BMOB API 的调用,所以有些代码可能看不懂,但是并不影响基本功能的使用。
   
    
//
//  NewsViewController.swift
//  WomenWorkerGuide
//
//  Created by why on 9/20/14.
//  Copyright (c) 2014 why. All rights reserved.
//
 
importUIKit
 
/**
*  新闻
*/
classNewsViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
 
    @IBOutletweak var newsTableView: UITableView!
     
    var refreshControl = UIRefreshControl()
     
    var dataArray:[AnyObject] = [AnyObject]()
     
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.automaticallyAdjustsScrollViewInsets = false
         
        //添加刷新
        refreshControl.addTarget(self, action: "refreshData", forControlEvents: UIControlEvents.ValueChanged)
        refreshControl.attributedTitle = NSAttributedString(string: "松开后自动刷新")
        newsTableView.addSubview(refreshControl)
        refreshData()
    }
 
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
     
    // 刷新数据
    func refreshData() {
        let bquery = BmobQuery(className: "News")
        bquery.findObjectsInBackgroundWithBlock({array, error in
            self.dataArray = array
            self.newsTableView.reloadData()
            self.refreshControl.endRefreshing()
        })
    }
 
    // MARK: - UITableViewDataSource
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        returndataArray.count;
    }
     
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
         
        let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier:"newsCell")
         
        let obj: BmobObject = dataArray[indexPath.row] as BmobObject
    
        cell.textLabel?.text = obj.objectForKey("title") as? String
        
         
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "yyyy年 MM月 dd日"
        let str = dateFormatter.stringFromDate(obj.createdAt)
        cell.detailTextLabel?.text = str
        returncell;
    }
     
     
     
     
    /*
    // MARK: - Navigation
 
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */
 
}



【官方头文件】

- ( instancetype )init;

@property ( nonatomic , readonly , getter =isRefreshing) BOOL refreshing;

@property ( null_resettable , nonatomic , strong ) UIColor *tintColor;
@property ( nullable , nonatomic , strong ) NSAttributedString *attributedTitle UI_APPEARANCE_SELECTOR ;

// May be used to indicate to the refreshControl that an external event has initiated the refresh action
- (
void )beginRefreshing NS_AVAILABLE_IOS ( 6 _0);
// Must be explicitly called when the refreshing has completed
- (
void )endRefreshing NS_AVAILABLE_IOS ( 6 _0);

@end






NS_CLASS_AVAILABLE_IOS ( 6 _0)  @interface  UIRefreshControl :  UIControl

// refreshControl初始化
- ( instancetype )init;

// 刷新中得状态判断,只读属性,根据状态可做一些自定义的事情
@property  ( nonatomic ,  readonly ,  getter =isRefreshing)  BOOL  refreshing;

// 菊花以及文字的颜色
@property  ( nonatomic ,  retain )  UIColor *tintColor;

// 下拉刷新文字描述,自定义
@property  ( nonatomic ,  retain )  NSAttributedString  *attributedTitle  UI_APPEARANCE_SELECTOR ;

// 开始刷新
- ( void )beginRefreshing  NS_AVAILABLE_IOS ( 6 _0);
// 结束刷新,在确定获得想要的加载数据之后调用
- ( void )endRefreshing  NS_AVAILABLE_IOS ( 6 _0);

@end


使用方法]
1 .目前只对UITableviewController有用;
2.只能下拉刷新,不能上拉刷新;
3.init或者viewdidload中创建 UIRefreshControl,设置文字,颜色等信息;
4.系统自动管理 UIRefreshControl,自动添加到tableview视图中;
5.给 UIRefreshControl添加方法,当值改变的时候调用,方法用于数据请求;
6.该方法中请求数据确认完成之后,调用 endRefreshing方法,关闭刷新;

【代码示例】


- (
void )creatRefreshing {
   
  self .refreshControl = [[ UIRefreshControl  alloc ]  init ];
   
  self .refreshControl. attributedTitle  = [[ NSAttributedString  alloc ]  initWithString : @" 努力加载中 ……" ];
   
  self .refreshControl. tintColor  = [ UIColor  grayColor ];
    [ self .refreshControl  addTarget : self  action : @selector (refreshAction) forControlEvents : UIControlEventValueChanged ];
}

- ( void )refreshAction{
    _reloadCount++;
   
  //  请求数据
    [
self  requestFromDPAPI ];
   
  //  结束刷新
    [
self .refreshControl  endRefreshing ];
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值