iOS Development UI article-load more features in UITableView

Source: Internet
Author: User
Tags uikit

First, the realization effect

Click Load More button, there is a load diagram, three seconds after the addition of two new data.

II. Implementation code and description

When you click the Load More button on the page (View section), the main page (Master Controller) loads two data in.

When a button in the View section is clicked, to allow the host controller to load the data and refresh the table, the 2B youth will add a property of the host controller to the view, which can be loaded by invoking this property, but this is usually done through proxy mode in development.

The following are two implementations of the code, respectively.

1. Project Structure and description

Description: Load more is always placed at the bottom of this tableview, so this is set as the TableView Tablefooterview.

Self.tableview.tablefooterview=footerview;

In the implementation through the XIB to handle, considering the left and right left white, and after the click to switch to the Load button and text, to control both the icon and text, so the loading icon and text hints in a view in order to control, This xib has been associated with Yyfooterview.xib, which controls xib through this class.

2. Implementation code

(1). Garbage code

Data Model Section

YYtg.h file

1//2//  YYtg.h 3//  02-Buy (use Xib and class to complete data display) 4//5//  Created by Apple on 14-5-29.6/  Copyright (c) 2014 Itcase. All rights reserved. 7//8  9 #import <foundation/foundation.h>10 #import "Global.h" @interface yytgmodel:nsobject13 @property (nonatomic,copy) NSString *icon;14 @property (nonatomic,copy) nsstring *buycount;15 @property (nonatomic,copy) nsstring *title;16 @ Property (nonatomic,copy) NSString *price;17 18//external interface Yyinith (TG) @end

YYTG.M file

1//2//  YYTG.M 3//  02-Buy (use Xib and class to complete data display) 4//5//  Created by Apple on 14-5-29.6/  Copyright (c) 2014 Year Itcase. All rights reserved. 7//8  9 #import "YYtgModel.h" @implementation YYtgModel12 yyinitm (TG) @end

Note: The Construction method interface and implementation code for the data-to-model section have been encapsulated by custom macros with parameters.

The encapsulation code is as follows:

1 #ifndef _0____________global_h 2 #define _0____________GLOBAL_H 3  4/** 5  *  custom macros with parameters 6 */  7 #define 
   
    yyinith (name)   -(Instancetype) Initwithdict: (Nsdictionary *) dict; 8 + (instancetype) name# #WithDict:(nsdictionary *) dict; 9 #define     YYINITM (name)  -(Instancetype) Initwithdict: (Nsdictionary *) dict12 {     if (self=[super init ]) {         [self setvaluesforkeyswithdictionary:dict];15     }16     return self;17}18 + (instancetype) name## Withdict: (nsdictionary *) dict20 {     return [[Self alloc]initwithdict:dict];22}23 #endif
   

View section

YYtgcell.h file

1//2//  YYtgcell.h 3//  02-Buy (use Xib and class to complete data display) 4//5//  Created by Apple on 14-5-29.6/  Copyright (c) 2 014 Years itcase. All rights reserved. 7//8  9 #import <uikit/uikit.h>10 #import "YYtgModel.h" @interface yytgcell:uitableviewcell13 @property ( Nonatomic,strong) Yytgmodel *yytg;14 15//Load data (encapsulated using XIB to create the internal details of the cell) + (Instancetype) Tgcellwithtableview: ( UITableView *) tableview;17 @end

YYTGCELL.M file

 1//2//YYTGCELL.M 3//02-Buy (use Xib and class to complete data display) 4//5//Created by Apple on 14-5-29. 6//Copyright (c) 2014 itcase. All rights reserved. 7//8 9 #import "YYtgcell.h" 10//Private extension one @interface Yytgcell () @property (Strong, nonatomic) Iboutlet Uiimageview *img;  @property (Strong, nonatomic) Iboutlet UILabel *titlelab;14 @property (Strong, nonatomic) Iboutlet UILabel *pricelab;15 @property (Strong, nonatomic) Iboutlet UILabel *buycountlab;16 @end17 @implementation YYtgcell18 #pragma mark overrides the Set method,     Completion of assignment of data-(void) SETYYTG: (Yytgmodel *) yytg21 {_yytg=yytg;23 self.img.image=[uiimage imagenamed:yytg.icon];24 self.titlelab.text=yytg.title;25 self.pricelab.text=[nsstring stringwithformat:@ "$%@", yytg.price];26 SELF.BUYC Ountlab.text=[nsstring stringwithformat:@ "has been purchased by%@", yytg.buycount];27}28 + (instancetype) Tgcellwithtableview: ( UITableView *) tableView30 {to static nsstring *identifier= @ "TG"; Yytgcell *cell=[tableview DEQUEUEREUSABLECELLW Ithidentifier:identifier];33 if (cell==nil) {34///How to add a stamp to the created cell 35//For the loaded Xib file, you can set it to the Xib view's property selector. =[[[nsbundle mainbundle]loadnibnamed:@ "Tgcell" Owner:nil options:nil]firstobject];37 NSLog (@ "created a Cell"); 38}3 9 Return cell;40}41 @end

YYfooterview.h file

1//2//  YYfooterview.h 3//  02-Group purchase (use Xib and class to complete data display) 4//5//  Created by Apple on 14-5-29.6//  Copyright ( c) 2014 Itcase. All rights reserved. 7//8  9 #import <uikit/uikit.h>10 @class yyviewcontroller;11 @interface yyfooterview:uiview12 @property (Nona Tomic,strong) Yyviewcontroller *controller;13 @end

YYFOOTERVIEW.M file

1//2//  YYFOOTERVIEW.M 3//  02-Group purchase (use Xib and class to complete data display) 4//5//  Created by Apple on 14-5-29.6//  Copyright ( c) 2014 Itcase. All rights reserved. 7//8  9 #import "YYfooterview.h" #import "YYViewController.h" @interface Yyfooterview () @property (Strong, nonatomic) Iboutlet Uiactivityindicatorview *loadingview;14 @property (Strong, nonatomic) Iboutlet UIButton *loadbtn;15 @end17 @implementation YYfooterview18-(ibaction) Loadbtclick {     NSLog (@ button was clicked);/     /Hide button     Self.loadbtn.hidden=yes;22     //show chrysanthemum     self.loadingview.hidden=no;24     #warning analog Send network request, hidden Chrysanthemum 26 after 3 seconds     Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (3.0 * nsec_per_sec)), Dispatch_get_main_queue (), ^{ +         //3. Invoke the control's load data method         [Self.controller loadmore];29         //4. Hide Chrysanthemum view         Self.loadingview.hidden = YES ;         5. Display button         Self.loadbtn.hidden = no;33     });}35 @end

Master Controller

YYViewController.h file

1//2//  YYViewController.h 3//  02-Buy (use Xib and class to complete data display) 4//5//  Created by Apple on 14-5-29.6//  Copyrig HT (c) 2014 itcase. All rights reserved. 7//8  9 #import <uikit/uikit.h>10 @interface yyviewcontroller:uiviewcontroller12//Open Interface//-(void) LOADMO Re;14 @end

YYVIEWCONTROLLER.M file

  1//2//YYVIEWCONTROLLER.M 3//02-Buy (use Xib and class to complete data display) 4//5//Created by Apple on 14-5-29. 6//Copyright (c) 2014 itcase.  All rights reserved.  7//8 9 #import "YYViewController.h" #import "YYtgModel.h" #import "YYtgcell.h" #import "YYfooterview.h" 13 @interface Yyviewcontroller () <UITableViewDataSource,UITableViewDelegate> @property (Strong, Nonatomic) Iboutlet UITableView *tableview; @property (strong,nonatomic) Nsmutablearray *TG; @end @implementation yyviewcontroller #pragma mark-load data method (void) Loadmore 24 {25//Create Model YY Tgmodel *tgmodel=[[yytgmodel Alloc]init]; [email protected] "food good on the table"; [email protected] "5ee372ff039073317a49af5442748071"; [email protected] "20"; [email protected] "10000"; 31//Add model to array [SELF.TG Addobject:tgmodel]; Yytgmodel *tgmodelq=[[yytgmodel Alloc]init]; [email protected] "dish good on table 1"; [Email protEcted] "5ee372ff039073317a49af5442748071"; Panax Notoginseng [email protected] "20"; [email protected] "10000"; [Self.tg ADDOBJECT:TGMODELQ]; 41//Refresh form [Self.tableview Reloaddata]; (void) viewdidload [viewdidload], SELF.TABLEVIEW.ROWHEIGHT=80.F; 49 50//Load Bottom View 51//Get data from Xib uinib *nib=[uinib nibwithnibname:@ "Yyfooterview" bundle:nil]; Yyfooterview *footerview=[[nib Instantiatewithowner:nil Options:nil] firstobject]; Self.tableview.tablefooterview=footerview; 55//Set control footerview.controller=self; * * * #pragma mark-lazy Loading-(Nsarray *) TG (_tg==nil) {nsstring *fullpath=[[nsbundle mainbund le]pathforresource:@ "Tgs.plist" oftype:nil]; Nsarray *temparray=[nsarray Arraywithcontentsoffile:fullpath]; Nsmutablearray *arraym=[nsmutablearray ArrayWithCapacity:temparray.count]; Nsdictionary *dict in Temparray{Yytgmodel *tg=[yytgmodel tgwithdict:dict]; [Arraym ADDOBJECT:TG]; 69} 70 _tg=arraym; _TG return; #pragma mark-xib create cell data processing #pragma mark how many sets of Nsinteger-(Numberofsectionsintableview): (UITableView *) TableView (1), Bayi} #pragma mark how many lines-(Nsinteger) TableView: (UITableView *) TableView numberofrows Insection: (Nsinteger) section-------------self.tg.count; the #pragma mark sets each row per line-(UITableViewCell *) Table View: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath 91 {92//1. Create cell, Yytgcell *cell= [Yytgcell Tgcellwithtableview:tableview]; 94 95//2. Gets the model of the current row, sets the data for the cell Yytgmodel *tg=self.tg[indexpath.row]; CELL.YYTG=TG;     98 99//3. Return cell100 return cell;101}102 103 #pragma mark-hidden status bar 104-(BOOL) prefersStatusBarHidden105 {106 Return yes;107}108 109 @end

2. Complete by proxy

When the button is clicked, the view part itself does not work, but notifies its agent (Controller) to complete the next operation.

This part of the code on the basis of 1 of the following several files are modified:

View section:

YYfooterview.h file

1//2//  YYfooterview.h 3//  02-Group purchase (use Xib and class to complete data display) 4//5//  Created by Apple on 14-5-29.6//  Copyright ( c) 2014 Itcase. All rights reserved. 7//8  9 #import <uikit/uikit.h>10 @class Yyviewcontroller, yyfooterview;11//Agreement @protocol Yyfooterviewdelegate <nsobject>13-(void) footerviewloadmore;14 @end15 @interface yyfooterview:uiview17 18// Declares an ID type attribute that complies with the Protocol's "person" to become its agent @property (Nonatomic,strong) id<yyfooterviewdelegate> delegate;20//@property ( Nonatomic,strong) Yyviewcontroller *controller;21 @end

YYFOOTERVIEW.M file

 1//2//YYFOOTERVIEW.M 3//02-Buy (use Xib and class to complete data display) 4//5//Created by Apple on 14-5-29. 6//Copyright (c) 2014 itcase. All rights reserved. 7//8 9 #import "YYfooterview.h" #import "YYViewController.h" @interface Yyfooterview () @property (Strong, non Atomic) Iboutlet Uiactivityindicatorview *loadingview;14 @property (Strong, nonatomic) Iboutlet UIButton *loadbtn;15 16 @ End17 @implementation YYfooterview18-(ibaction) Loadbtclick {NSLog (@ "button was clicked"); 20//Hide button Self.loadbtn.hid DEN=YES;22//Show chrysanthemum self.loadingview.hidden=no;24 #warning analog Send network request, 3 seconds after hiding Chrysanthemum Dispatch_after (dispatch        _time (Dispatch_time_now, (int64_t) (3.0 * nsec_per_sec)), Dispatch_get_main_queue (), ^{27//3. Invoking the control's load data method 28//         [Self.controller loadmore];29//Notification agent [Self.delegate footerviewloadmore];31//4. Hide Chrysanthemum View 32 Self.loadingview.hidden = yes;33//5. Display button Self.loadbtn.hidden = no;35});}37 @end

Master Controller Section

YYViewController.h file

1//2//  YYViewController.h 3//  02-Buy (use Xib and class to complete data display) 4//5//  Created by Apple on 14-5-29.6//  Copyrig HT (c) 2014 itcase. All rights reserved. 7//8  9 #import <uikit/uikit.h>10 @interface yyviewcontroller:uiviewcontroller12//Open Interface//-(void) LOADMO Re;14 @end

YYVIEWCONTROLLER.M file

  1//2//YYVIEWCONTROLLER.M 3//02-Buy (use Xib and class to complete data display) 4//5//Created by Apple on 14-5-29. 6//Copyright (c) 2014 itcase.  All rights reserved.  7//8 9 #import "YYViewController.h" #import "YYtgModel.h" #import "YYtgcell.h" #import "YYfooterview.h" 13 @interface Yyviewcontroller () <UITableViewDataSource,UITableViewDelegate,YYfooterviewDelegate> 15 @ Property (Strong, Nonatomic) Iboutlet UITableView *tableview; @property (strong,nonatomic) Nsmutablearray *TG;  @end @implementation yyviewcontroller #pragma mark-load data method (void) Footerviewloadmore 24 {25//Create Model Yytgmodel *tgmodel=[[yytgmodel Alloc]init]; [email protected] "food good on the table"; [email protected] "5ee372ff039073317a49af5442748071"; [email protected] "20"; [email protected] "10000"; 31//Add model to array [SELF.TG Addobject:tgmodel]; Yytgmodel *tgmodelq=[[yytgmodel Alloc]init]; [Email protected] "Dish Good on table 1 "; [email protected] "5ee372ff039073317a49af5442748071"; Panax Notoginseng [email protected] "20"; [email protected] "10000"; [Self.tg ADDOBJECT:TGMODELQ]; 41//Refresh form [Self.tableview Reloaddata]; //-(void) Loadmore 45//{46////Create model//Yytgmodel *tgmodel=[[yytgmodel alloc]init];//[EMAIL&NBSP ;p rotected] "Good food on the table"; //[email protected] "5ee372ff039073317a49af5442748071"; [email protected] "20"; Wuyi//[email protected] "10000"; 52////Add model to array//[SELF.TG Addobject:tgmodel]; Wu///Yytgmodel *tgmodelq=[[yytgmodel Alloc]init]; [email protected] "dish good on table 1"; //[email protected] "5ee372ff039073317a49af5442748071"; +//[email protected] "20"; //[email protected] "10000"; /////[SELF.TG ADDOBJECT:TGMODELQ]; 62////Refresh form//[Self.tableview reloaddata]; +//}-(void) viewdidload, [the Super Viewdidload]; SELF.TABLEVIEW.ROWHEIGHT=80.F; 70 71//Load Bottom view 72//Get data from Xib uinib *nib=[uinib nibwithnibname:@ "Yyfooterview" bundle:nil]; Yyfooterview *footerview=[[nib Instantiatewithowner:nil Options:nil] firstobject]; Self.tableview.tablefooterview=footerview; 76//Set control footerview.controller=self//; 78//Set Agent footerview.delegate=self; I #pragma mark-lazy loading-(Nsarray *) TG (_tg==nil) (NSString *fullpath=[[nsbundle Mainbund le]pathforresource:@ "Tgs.plist" oftype:nil]; Nsarray *temparray=[nsarray Arraywithcontentsoffile:fullpath]; Nsmutablearray *arraym=[nsmutablearray ArrayWithCapacity:temparray.count];             Nsdictionary *dict in Temparray) {Yytgmodel *tg=[yytgmodel tgwithdict:dict]; 91 [Arraym ADDOBJECT:TG]; _tg=arraym}; 94} _TG return; 98 #pragma mark-xib create cell data processing #pragma mark How many groups 101-(Nsinteger) Numberofsectionsintableview: (UITableView *) tableView102 {103 return 1;104}105 106 #pragma mark how many lines 107-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (Nsinteger) section108 {109 return self.tg.count;110}111 #pragma mark set each row 113-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexPath114 {115//1. Create cell116 Yytgcell *cell=[yytgcell Tgcellwithtableview:     tableview];117 118//2. Gets the model of the current row, sets the cell's data 119 Yytgmodel *tg=self.tg[indexpath.row];120 cell.yytg=tg;121  122//3. Return cell123 return cell;124}125 126 #pragma mark-hidden status bar 127-(BOOL) prefersStatusBarHidden128 {129 return yes;130}131 @end

iOS Development UI article-load more features in UITableView

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.