Implementation of pull refresh data under Swift development (using Uirefreshcontrol)

Source: Internet
Author: User
Tags current time

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
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.