The previous article (Swift-dropdown) Refreshes the functionality of the data (using Uirefreshcontrol) to refer to the ability to use Uirefreshcontrol to easily implement the dropdown refresh, through Attributedtitle property to set the descriptive text for the Drop-down.
But if you want to display different text depending on the status, such as drop down to refresh data, display "Data loading ..." When you refresh the data. Then you need to dynamically set up the Attributedtitle.
The effect chart is as follows:
Implementation mode:
(1) Modify Attributedtitle to "data loading ..." In the data refresh response method.
(2) Change the attributedtitle back to "drop refresh data" in the way scrollviewwillbegindragging the view starts scrolling
The code is as follows:
(For better see effect, simulate network request, here use Nstimer delay two seconds to generate data)
The code is as follows |
Copy Code |
Import Uikit Class Viewcontroller:uiviewcontroller,uitableviewdelegate,uitableviewdatasource {
News list @IBOutlet weak var newstableview:uitableview!
News Array Collection var dataarray:[hanggearticle] = [Hanggearticle] ()
Pull Refresh Controller var Refreshcontrol = Uirefreshcontrol ()
var timer:nstimer! Override Func Viewdidload () { Super.viewdidload ()
Self.automaticallyadjustsscrollviewinsets = False
Add Refresh Refreshcontrol.addtarget (Self, Action: "RefreshData", forControlEvents:UIControlEvents.ValueChanged) Refreshcontrol.attributedtitle = nsattributedstring (string: "Drop Refresh Data") Newstableview.addsubview (Refreshcontrol) RefreshData () }
Scrolling view starts dragging Func scrollviewwillbegindragging (Scrollview:uiscrollview) { If!refreshcontrol.refreshing { Refreshcontrol.attributedtitle = nsattributedstring (string: "Drop Refresh Data") } } Refreshing data Func RefreshData () { Refreshcontrol.attributedtitle = nsattributedstring (string: "Data Loading ...") Timer = nstimer.scheduledtimerwithtimeinterval (2.0, Target:self, Selector: "TimeOut", Userinfo:nil, Repeats:true) }
Timer time to Func TimeOut () { removing old data Self.dataArray.removeAll () Randomly add 5 new data (time is current time) For _ in 0..<5 { Let atricle = hanggearticle (title: "News title \ (Int (Arc4random ()%1000))", Createdate:nsdate ()) Self.dataArray.append (atricle) } Self.newsTableView.reloadData () Self.refreshControl.endRefreshing ()
Timer.invalidate () Timer = Nil }
Return number of records Func TableView (Tableview:uitableview, numberofrowsinsection section:int)-> Int { return dataarray.count; }
Return cell contents Func TableView (Tableview:uitableview, Cellforrowatindexpath Indexpath:nsindexpath) -> UITableViewCell { Let cell = UITableViewCell (Style:UITableViewCellStyle.Subtitle, Reuseidentifier: "MyCell")
Set cell title Let atricle:hanggearticle = Dataarray[indexpath.row] as Hanggearticle Cell.textlabel? Text = Atricle.title
Set cell subtitle Let Dateformatter = NSDateFormatter () Dateformatter.dateformat = "Yyyy-mm-dd HH:mm:ss" Let str = dateformatter.stringfromdate (atricle.createdate) Cell.detailtextlabel? Text = str
return cell; }
Override Func didreceivememorywarning () { Super.didreceivememorywarning ()
} }
News structure Body struct Hanggearticle { var title:string var createdate:nsdate } |
All right, here's a small set of Swift's Uirefreshcontrol, when refreshing, use different examples of descriptive text, hoping to help you.