Swift iOS : 上拉重新整理或者下拉重新整理

RecoReco發表於2017-05-26

框架MJRefresh提供了上拉重新整理或者下拉重新整理,非常的喜聞樂見。

需要使用pod:

target 'Swift-MJrefresh' do
  use_frameworks!
  # Pods for Swift-MJrefresh
  pod 'MJRefresh'
end複製程式碼

記得去執行命令安裝此框架:

pod install --verbose --no-repo-update複製程式碼

如下案例,演示此框架的使用過程:

import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window : UIWindow?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        window = UIWindow()
        window!.rootViewController = Page()
        window!.rootViewController!.view.backgroundColor = .blue
        window!.makeKeyAndVisible()
        return true
    }
}
import MJRefresh
class Page: UIViewController ,UITableViewDataSource{
    var tableview: UITableView!
    let header = MJRefreshNormalHeader()
    let footer = MJRefreshAutoNormalFooter()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableview = UITableView()
        tableview.dataSource = self
        tableview.frame = view.frame
        self.view.addSubview(tableview)
        header.setRefreshingTarget(self, refreshingAction: Selector("headerRefresh"))
        self.tableview.mj_header = header
        footer.setRefreshingTarget(self, refreshingAction: Selector("footerRefresh"))
        self.tableview.mj_footer = footer
    }
    func headerRefresh(){
        print("下拉重新整理")
        self.tableview.mj_header.endRefreshing()
    }
    var index = 0
    func footerRefresh(){
        print("上拉重新整理")
        self.tableview.mj_footer.endRefreshing()
        // 2次後模擬沒有更多資料
        index = index + 1
        if index > 2 {
            footer.endRefreshingWithNoMoreData()
        }
    }
    func numberOfSections(in: UITableView) -> Int {
        return 1;
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10;
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "a")
        cell.textLabel!.text = "測試重新整理"
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAtIndexPath indexPath: IndexPath) -> CGFloat {
        return 150;
    }
}複製程式碼

實驗發現,即使是Obj-c寫的框架,也只要:

import MJRefresh複製程式碼

而不再需要橋接檔案(bridge.h)。以前都是需要的,為什麼不需要了?還需要進一步查明。

相關文章