First, initialize the relevant data in Viewdidload:
Override Func Viewdidload () { super.viewdidload () //Do any additional setup after loading the view. Add Refresh refreshcontrol.addtarget (self, Action: "RefreshData", forControlEvents:UIControlEvents.ValueChanged) Refreshcontrol.attributedtitle = nsattributedstring (string: "Let go Refresh News") Newstableview.addsubview ( Refreshcontrol) }
So the drop-down refreshes are already visible:
Next, refine the selector and add the RefreshData method:
Refresh Data func RefreshData () {let bquery = Bmobquery (className: "News") Bquery.findobjectsinbackgroundwithblock ({array, error in Self.dataarray = array Self.newsTableView.reloadData () self.refreshControl.endRefreshing ()} ) }
The complete code is as follows, because it involves calls to the Bmob API, so some code may not be readable, but it does not affect the use of basic functionality.
newsviewcontroller.swift//womenworkerguide////Created on 9/20/14.//Copyright (c). All rights Reserved.//import uikit/*** news */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//Add refresh Refreshcontrol.addtarget (self, Action: "ref Reshdata ", forControlEvents:UIControlEvents.ValueChanged) Refreshcontrol.attributedtitle = nsattributedstring (stri NG: "Auto refresh after release") Newstableview.addsubview (Refreshcontrol) RefreshData ()} override Func Didreceivememory Warning () {super.didreceivememorywarning ()//Dispose of any resources the can be recreated. } Refresh Data func RefreshData () {Let bquery = Bmobquery (className: "News") BQUERY.FINDOBJECTSINBACKGROUNDW Ithblock ({array, error in Self.dataarray = array self.newsTableView.reloadData () self.ref Reshcontrol.endrefreshing ()})}//MARK:-uitableviewdatasource func TableView (Tableview:uitableview, n Umberofrowsinsection section:int), Int {return dataarray.count; } func TableView (Tableview:uitableview, Cellforrowatindexpath indexpath:nsindexpath), UITableViewCell { Let cell = UITableViewCell (Style:UITableViewCellStyle.Subtitle, Reuseidentifier: "Newscell") le T obj:bmobobject = Dataarray[indexpath.row] as Bmobobject Cell.textlabel?. Text = Obj.objectforkey ("title") as? String Let Dateformatter = NSDateFormatter () Dateformatter.dateformat = "yyyy year mm DD Day" Let str = dateformatter.stringfromdate (obj.createdat) CELl.detailtextlabel?. Text = str return cell; }/*//MARK:-Navigation//In a storyboard-based application, you'll often want to do a Litt Le 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. } */}
[IOS] using Uirefreshcontrol for UITableView pull-down refresh (Swift version)