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
3. The code is as follows
Import Uikit class Viewcontroller:uiviewcontroller,uitableviewdelegate,uitableviewdatasource {//News list @IBOutlet Weak V
Ar newstableview:uitableview! News array set var dataarray:[hanggearticle] = [Hanggearticle] ()//Pull Refresh controller var Refreshcontrol = Uirefreshcontrol () override F UNC Viewdidload () {super.viewdidload () Self.automaticallyadjustsscrollviewinsets = false//Add Refresh Refreshcontrol.ad Dtarget (Self, Action: "RefreshData", forControlEvents:UIControlEvents.ValueChanged) refreshcontrol.attributedtitle = Nsattributedstring (string: "Drop-down Refresh Data") Newstableview.addsubview (Refreshcontrol) RefreshData ()}//Refresh data func refres Hdata () {//Remove old data self.dataArray.removeAll ()//Randomly add new data (time is current time) for _ in.. < {Let atricle = hanggearticle (title: "News title \ (Int (Arcrandom ())", Createdate:nsdate ()) Self.dataArray.appen D (atricle)} self.newsTableView.reloadData () self.refreshControl.endRefreshing ()}//return record number func TableView (Tablev Iew:uitableview, NumberofrowsiNsection section:int)-> Int {return dataarray.count; }//Return cell content func TableView (Tableview:uitableview, Cellforrowatindexpath indexpath:nsindexpath)-> UITableViewCel l {Let cell = UITableViewCell (Style:UITableViewCellStyle.Subtitle, Reuseidentifier: "MyCell")//Set cell title let ATR Icle:hanggearticle = Dataarray[indexpath.row] as hanggearticle Cell.textlabel? Text = atricle.title//Set cell subtitle Let Dateformatter = NSDateFormatter () Dateformatter.dateformat = "Yyyy-mm-dd HH:mm:s S "Let str = dateformatter.stringfromdate (atricle.createdate) Cell.detailtextlabel?"
Text = str return cell; Override Func didreceivememorywarning () {super.didreceivememorywarning ()}}//News structure struct Hanggearticle {var ti tle:string var createdate:nsdate}
The use of Ps:uirefreshcontrol
1, the use of the scope
If you put Xcode_4.5_developer_preview, So in the UITableViewController.h file you'll see that Uitableviewcontroller has the following declaration that Uitableviewcontroller has built-in Uirefreshcontrol controls
Copy Code code as follows:
@property (nonatomic,retain) Uirefreshcontrol *refreshcontrol Ns_available_ios (6_0);
"Note": Uirefreshcontrol is currently available only for Uitableviewcontroller, if used in other Viewcontroller, The runtime will get the following error prompt: (that is, Uirefreshcontrol can only be Uitableviewcontroller managed)
2012-06-15 14:34:34.908 devdivuirefreshcontrol[722:10103] * * * terminating app due to uncaught exception ' Nsinternalinconsistencyexception ', Reason: ' Uirefreshcontrol May is managed by a Uitableviewcontroller '
* * * Throw Call stack:
(0x186fd72 0x1066e51 0x186fb4b 0x55a559 0x57238 0x5d482 0x55ad2 0x2ebb 0xeb2a3 0xeb30e 0x10b7e 9 0x10b624 0x109aef 0x10999c 0x107adc 0x1082c6 0xecf24 0xed1e0 0xee084 0x5645c 0x5cf31 0x55ad2 0x4131d 0x414f6 0x4168c 0x4 9871 0x10a90 0x1196a 0x222be 0x22f9f 0x153fd 0x17ccf39 0x17ccc10 0x17e5da5 0x17e5b12 0x1816b46 0x1815ed4 0x1815dab 0x1128f 0x12e71 0x29fd 0x2925)
Libc++abi.dylib:terminate called throwing an exception
(LLDB)
2. How to use
a) initialization
How to use Uirefreshcontrol in Uitableviewcontroller, in the code attachment given above, you can know in detail, here are the key parts:
Self.refreshcontrol = [[Uirefreshcontrol alloc]init];
Self.refreshControl.tintColor = [Uicolor bluecolor];
Self.refreshControl.attributedTitle = [[nsattributedstring alloc]initwithstring:@ "Drop-down Refresh"];
As seen above the code, although Uitableviewcontroller has declared Uirefreshcontrol, but it seems not initialized, so we need to initialize ourselves. It's amazing that when you initialize, you don't need to give it a frame,uitableviewcontroller that will manage it for us. Unfortunately, at present only see the Drop-down refresh function, pull refresh has not yet, estimated in the final version of Apple will consider adding a pull refresh function.
We can also set Tintcolor and Attributedtitle for Uirefreshcontrol.
b) pull down Refresh Event Monitor
When the user makes a drop-down refresh operation, Uirefreshcontrol triggers a uicontroleventvaluechanged event, and by listening to the event, we can do a similar data request operation. The following code:
Copy Code code as follows:
[Self.refreshcontrol addtarget:self Action: @selector (refreshviewcontroleventvaluechanged)
c) Request for data
In the example, in order to demonstrate the data request, I simply do a delay processing, 2 seconds later, call Handledata
Copy Code code as follows:
[Self performselector: @selector (handledata) Withobject:nil Afterdelay:2];
In Handledata, it means that the data has been requested, and UI updates can be made here. Also note that we call the Uirefreshcontrol endrefreshing method, which indicates that the refresh is over and let Uirefreshcontrol update the display.
-(void) handledata
{
NSLog (@ "refreshed");
[Self.refreshcontrol endrefreshing];
Self.refreshControl.attributedTitle = [[nsattributedstring alloc]initwithstring:@ "Drop-down refresh"];
self.count++;
[Self.tableview Reloaddata];
3. Official Head Documents
The following is the Uirefreshcontrol statement in the SDK, presumably looking at the following code, and you already know how to use it.
//UIRefreshControl.h
//Uikit////
Copyright in Apple Inc. All rights reserved.
#import <Foundation/Foundation.h>
#import <UIKit/UIControl.h>
#import <uikit/ Uikitdefines.h>
Ns_class_available_ios (6_0) @interface Uirefreshcontrol:uicontrol/
* The designated Initializer
* This Initializes a uirefreshcontrol with a default height and width.
* Once assigned to a Uitableviewcontroller, the frame of the control is managed automatically.
* When a user has Pulled-to-refresh, the Uirefreshcontrol fires its Uicontroleventvaluechanged event.
* *
-(ID) init;
@property (nonatomic, ReadOnly, getter=isrefreshing) BOOL refreshing;
@property (nonatomic, retain) uicolor *tintcolor ui_appearance_selector;
@property (n