When Swift Uirefreshcontrol Drop-down, use different descriptive text when refreshing

Source: Internet
Author: User
Tags current time

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.

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.