To refresh the table data, pull up the new data, there are many Third-party implementation classes on the Web.
And if only need to realize the Drop-down refresh data, then the use of Uirefreshcontrol is sufficient, simple and useful.
1,uirefreshcontrol Steps to use:
(1) Create Uirefreshcontrol, and set text, color and other information.
(2) Add Uirefreshcontrol to the TableView view.
(3) Add a method to the Uirefreshcontrol, change the value of the call, for data request refresh.
(4) After the request data confirmation completes, calls the Endrefreshing method, turns off the refresh.
2, the effect chart is as follows
The code is as follows
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 ()
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 ()
}
Refreshing data
Func RefreshData () {
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 ()
}
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
}