IOS: drop-down refresh control Uirefreshcontrol

Source: Internet
Author: User

Drop-down refresh control: Uirefreshcontrol


1, the specific type of information:

@interface Uirefreshcontrol:uicontrol/ /Inheritance Control class

-(instancetype) init;

@property (nonatomic, ReadOnly, getter=isrefreshing) BOOL refreshing; //Whether you can refresh

@property (Nonatomic, retain) Uicolor *tintcolor; //Control color

@property (nonatomic, retain) nsattributedstring *attributedtitle; //control property title

-(void) beginrefreshing; //Start Refresh

-(void) endrefreshing; //End Refresh

@end

2. Use:

It is typically used to refresh downloaded data and display it, such as a refresh when surfing the Internet. Of course, it can be paired with the table View TableView, and the table data can be refreshed to load the downloaded data when it is pulled down. In addition, it is also a unique property of the table View controller Uitableviewcontroller.

Here are two examples to refresh the data in the display table: Example 1: By inheriting the table View controller Uitableviewcontroller to use the drop-down to refresh the control Uirefreshcontrol, TableView and Refreshcontrol refresh the control as a unique property of the table View Controller. Pre-Storyboard layout processing:<1> Remove the controller from the storyboard and drag it into a uitableviewcontroller<2> inherit the Viewcontroller class from the table View Controller class Uitableviewcontroller<3> associating the Uitableviewcontroller controller with the corresponding class ViewcontrollerWell, the next step is to write the code//1. Defines a mutable array to store the loaded data
1 #import " ViewController.h " 2 @interface Viewcontroller () 3 @property (strong,nonatomic) nsmutablearray *Arraym; 4 @end

2. Lazy Loading Initialize Array

1 -(nsmutablearray*) Arraym2{3     if(!  _arraym)4    {5         _arraym = [Nsmutablearray array]; 6     }7     return  _arraym; 8 }

3. Initialize the Refresh control and add control events

1- (void) Viewdidload {2 [Super Viewdidload];3     4     //To create a refresh control5Self.refreshcontrol = [[Uirefreshcontrol alloc]initwithframe:cgrectmake (0,0, Self.tableView.frame.size.width, -)];6     7     //to add a refresh control to the table view header8 [Self.tableView.tableHeaderView AddSubview:self.refreshControl];9     Ten     //Add Refresh Event One [Self.refreshcontrol addtarget:self Action: @selector (LoadData:) forcontrolevents:uicontroleventvaluechanged]; A}

4. Encapsulating a method for loading data

1-(void) Preparedata2 {3      for(intI=0; i<3; i++)4     {5NSString *product = [NSString stringWithFormat:@"Product-%d", Arc4random_uniform (Ten)];6 [Self.arraym addobject:product];7         8         //Overall Refresh Table9 [Self.tableview Reloaddata];Ten     } One}

5. Implement events that refresh the control

1 #pragma mark-loaddata 2 -(void) LoadData: (uirefreshcontrol*) sender3{4     //  Load Data 5    [self preparedata]; 6     7     // End Refresh 8     [Self.refreshcontrol endrefreshing]; 9 }

6, the implementation of Table view data source protocol related methods, display data in the table

1 #pragma mark-<uitableviewdatasource>2 //Number of rows3-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section4 {5     returnSelf.arrayM.count;6 }7 //set the contents of each cell8-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath9 {Ten     //1. According to Reuseidentifier, first go to the object pool to find the reused Cell object One     StaticNSString *reuseidentifier =@"Cell"; AUITableViewCell *cell =[Self.tableview Dequeuereusablecellwithidentifier:reuseidentifier]; -     //2. If not found, create a Cell object yourself -     if(Cell = =Nil) the     { -Cell =[[UITableViewCell Alloc]initwithstyle:uitableviewcellstyledefault reuseidentifier:reuseidentifier]; -     } -     //3. Set the contents of a Cell object +Cell.textLabel.text =[Self.arraym ObjectAtIndex:indexPath.row]; -     returncell; +}

The demo results are as follows:

No action: When a drop-down is refreshed: three more data

Example 2: Pure code creates tableview and Uirefreshcontrol in the Viewcontroller class, and then implements a drop-down refresh operation to display the results
The specific code is as follows://1. Implementing a data source protocol and defining required properties
1 #import " ViewController.h " 2 3 @interface Viewcontroller () <UITableViewDataSource>4 @property (strong,nonatomic) UITableView *TableView; 5 @property (strong,nonatomic) Uirefreshcontrol *Refreshcontrol; 6 @property (strong,nonatomic) nsmutablearray *Arraym; 7 @end

The 2.-(void) Viewdidload method completes the following code:

※ Create a table view and add it to the view view

1     Self.tableview = [[UITableView alloc]initwithframe:self.view.bounds]; 2     [Self.view AddSubview:self.tableView];

※ Create a Refresh control and add it to the table view

1 self.refreshcontrol = [[Uirefreshcontrol alloc]initwithframe:cgrectmake (00100 )]; 2 self.refreshControl.tintColor = [Uicolor redcolor]; 3 [Self.tableview AddSubview:self.refreshControl];

※ Initialize the array and prepare the data

1  Self.arraym = [Nsmutablearray array]; 2      for (int i=0; i<5; i++) 3     {4         nsstring *book = [NSString stringWithFormat:@ "book-%d ", i+1]; 5         [Self.arraym Addobject:book]; 6     }

※ Set up a data source and add Refresh control events

1 self.tableView.dataSource = self ; 2 [Self.refreshcontrol addtarget:self action: @selector (load:) forcontrolevents:uicontroleventvaluechanged ];

3. Implement Refresh Event Load data

1 #pragma mark-load2-(void) Load: (uirefreshcontrol*) Sender3 {4     //Start refreshing load data5NSString *room = [NSString stringWithFormat:@"room-%d", Arc4random_uniform (Ten)];6 [Self.arraym addobject:room];7 [Self.tableview Reloaddata];8     9     //End RefreshTen [Self.refreshcontrol endrefreshing]; One}

4, the realization of the data source protocol needs of the method, display data re-form

1 #pragmaMark-<uitableviewdatasource>2 //Line3-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section4 {5     returnSelf.arrayM.count;6 }7 //set the contents of each cell8-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath9 {Ten     //1. According to Reuseidentifier, first go to the object pool to find the reused Cell object One     StaticNSString *reuseidentifier =@"Cell"; AUITableViewCell *cell =[Self.tableview Dequeuereusablecellwithidentifier:reuseidentifier]; -     //2. If not found, create a Cell object yourself -     if(Cell = =Nil) the     { -Cell =[[UITableViewCell Alloc]initwithstyle:uitableviewcellstyledefault reuseidentifier:reuseidentifier]; -     } -     //3. Set the contents of a Cell object +Cell.textLabel.text =[Self.arraym ObjectAtIndex:indexPath.row]; -     returncell; +}

The demo results are as follows:

Without any action: when the drop-down is refreshed:

After the refresh: one more room-1 data

IOS: drop-down refresh control Uirefreshcontrol

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.