你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> [iOS] 使用UIRefreshControl 實現 UITableView下拉刷新(Swift版本)

[iOS] 使用UIRefreshControl 實現 UITableView下拉刷新(Swift版本)

編輯:IOS開發綜合

首先,在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)
        
    }

這樣下拉刷新已經可以看到了:

\



<喎?/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.
//

import UIKit

/**
*  新聞
*/
class NewsViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    @IBOutlet weak 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 {
        return dataArray.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
        return cell;
    }
    
    
    
    
    /*
    // 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.
    }
    */

}



  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved