MJRefresh的使用

1⃣写一个简单的tableview模版,显示数组里的数据

  

    var ObjectArray = [String]()
    var i = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()

        for i = 0 ; i<10 ; i++ {
            self.ObjectArray.append("\(i)")
        }
    }

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return ObjectArray.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
        cell.textLabel?.text = "这是第\(self.ObjectArray[indexPath.row])行"
        return cell
    }

2⃣添加MJRefresh,并创建桥接文件

  将MJRefresh拖到项目中:

  

  创建桥接文件:

  new file->source->Header file

  

  在项目-building settings中查找“bridging”设置添加的头文件的路径

  

  并在.h文件中引入第三方库

  #import "MJRefresh.h"

 

3⃣调用刷新方法

    override func viewDidLoad() {
        super.viewDidLoad()

        for i = 0 ; i<10 ; i++ {
            self.ObjectArray.append("\(i)")
        }
        
        self.tableView.addLegendHeaderWithRefreshingTarget(self,refreshingAction: "headRefresh")
        self.tableView.addGifFooterWithRefreshingTarget(self, refreshingAction: "footRefresh")
    }

    func headRefresh(){
        self.Delay(1, closure: { () -> () in
            self.ObjectArray.removeAll(keepCapacity: false)
            for self.i = 0 ; self.i < 10 ; self.i++ {
                self.ObjectArray.append("\(self.i)")
            }
                self.tableView.header.endRefreshing()
                self.tableView.reloadData()
            //self.head.stopRefresh()
            })
    }

    func footRefresh(){
            self.Delay(1, closure: { () -> () in
            let j = self.i + 10
            for self.i = 10 ; self.i < j ; self.i++ {
                self.ObjectArray.append("\(self.i)")
            }
                self.tableView.footer.endRefreshing()
                self.tableView.reloadData()
            })
    }
    //延迟函数,模拟服务器延迟
    func Delay(time:Double,closure:()->()){
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(time * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), closure)
    }

 

posted on 2015-12-22 09:29  罗小夕  阅读(677)  评论(0编辑  收藏  举报

导航