iOS eighth day (1:uitableviewcontroller buy, click Bottom, xib load more, proxy mode)

Source: Internet
Author: User
Tags uikit

HMViewController.h

#import "HMViewController.h"#import "HMTg.h"#import "HMTgCell.h"#import "HMTgFooterView.h"@interfaceHmviewcontroller () <HMTgFooterViewDelegate>@property (nonatomic, strong) Nsmutablearray*TGS;@end@implementationHmviewcontroller-(Nsarray *) tgs{if(_tgs = = nil) _tgs =[Hmtg TGS]; Data transfer Modelreturn_tgs;}- (void) viewdidload{[Super Viewdidload]; Self.tableView.rowHeight= the; //adjust the margins to make the table view stand out of the status barSelf.tableView.contentInset = Uiedgeinsetsmake ( -,0,0,0); //Footerview//the width of the footerview is consistent with the overall width of the table, only the height can be specified//Self.tableView.tableFooterView = [UIButton buttonwithtype:uibuttontypecontactadd];//UIView *view = [[UIView alloc] Initwithframe:cgrectmake (0, 0, 0,)];//View.backgroundcolor = [Uicolor redcolor];//Self.tableView.tableFooterView = view; //load last view from Xib set to FooterviewHmtgfooterview *footer =[Hmtgfooterview Footerview]; //View Controller becomes a proxy for FooterviewFooter.Delegate=Self ; Self.tableView.tableFooterView=footer; Self.tableView.tableHeaderView= [[[[NSBundle Mainbundle] loadnibnamed:@"Hmtgheadview"Owner:nil Options:nil] lastobject];}///* * Hide status bar *///-(BOOL) Prefersstatusbarhidden//{//return YES;//}#pragmaProxy methods for Mark-footerview/** Preprocessing Instructions #if 0 All code will not be executed #endif*/#if1-(void) Tgfooterviewdiddownloadbuttonclick: (Hmtgfooterview *) Footerview//Agent-to-method{    //Loading DataNSLog (@"try to load data in ...."); //add data to the array to simulate the effect of the network after it is loadedNsdictionary *dict = @{@"title":@"haha",@"icon":@"ad_00",@" Price":@"100.2",@"Buycount":@" -"}; HMTG*TG =[Hmtg tgwithdict:dict]; NSLog (@"add data before%d", Self.tgs.count);    [Self.tgs ADDOBJECT:TG]; NSLog (@"add data after%d", Self.tgs.count); //Refresh Data//[Self.tableview Reloaddata]; //Create a new IndexpathNsindexpath *path = [Nsindexpath indexpathforrow: (Self.tgs.count-1) Insection:0]; [Self.tableview Insertrowsatindexpaths:@[path] withrowanimation:uitableviewrowanimationmiddle];}#endif#pragmaMark-Data Source Method-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{returnSelf.tgs.count;}-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{//1. Create a cellHmtgcell *cell =[Hmtgcell Cellwithtableview:tableview]; //2. Through the data model, set the cell content, you can let the view controller does not need to understand the implementation details inside the cell    CELL.TG=Self.tgs[indexpath.row]; returncell;}@end

hmtgcell.m

#import "HMTgCell.h"#import "HMTg.h"@interfaceHmtgcell () @property (weak, nonatomic) Iboutlet Uiimageview*IconView, @property (weak, nonatomic) Iboutlet UILabel*Titlelabel, @property (weak, nonatomic) Iboutlet UILabel*Pricelabel, @property (weak, nonatomic) Iboutlet UILabel*Buycountlabel;@end@implementationHmtgcell+ (Instancetype)Cellwithtableview:(UITableView *) tableview{//1. Reusable identifiers    StaticNSString *id =@"Cell"; //2. TableView Query Reusable CellHmtgcell *cell =[TableView Dequeuereusablecellwithidentifier:id]; //3. If there are no reusable cells    if(Cell = =Nil) {NSLog (@"Load Xib"); //load a custom view from Xibcell = [[[NSBundle Mainbundle] loadnibnamed:@"Hmtgcell"Owner:nil Options:nil] lastobject]; }        returncell;}-(void) SETTG: (HMTG * ) TG {    //in the Setter method, the first sentence is assigned, otherwise the model will be used in other methods and the_TG =TG; Self.titleLabel.text=Tg.title; Self.iconView.image=[UIImage ImageNamed:tg.icon]; Self.priceLabel.text=Tg.price; Self.buyCountLabel.text=Tg.buycount;}#pragmaThe method provided by the mark-template/** The initialization method is called when the cell is created using code, and if you use Xib or storyboard, this method will not be called*/- (ID) Initwithstyle: (Uitableviewcellstyle) style Reuseidentifier: (NSString *) reuseidentifier{ Self=[Super Initwithstyle:style Reuseidentifier:reuseidentifier]; if(self) {NSLog (@"%s", __func__); }    returnSelf ;}/** After being loaded from xib, it will be automatically called and will not be executed if pure code is used*/- (void) awakefromnib{NSLog (@"%s", __func__); Self.contentView.backgroundColor=[Uicolor Clearcolor];}/** Cell is selected or unchecked it will be called if a custom cell control, all child controls should be added to Contentview*/- (void) setselected: (BOOL) selected animated: (BOOL) animated{[Super setselected:selected animated:animated]; if(selected) {Self.contentView.backgroundColor=[Uicolor Redcolor]; } Else{Self.contentView.backgroundColor=[Uicolor Greencolor]; }}@end

HMTgCell.h

#import <UIKit/UIKit.h>@class  hmtg; @interface Hmtgcell:uitableviewcell /*  */@property (nonatomic, strong)*TG; /*  */+ (Instancetype) Cellwithtableview: (UITableView *) TableView; @end

HMTgFooterView.h

#import<UIKit/UIKit.h>@classHmtgfooterview;@protocolHmtgfooterviewdelegate <NSObject>@optional/** The download button of the view is clicked*/- (void) Tgfooterviewdiddownloadbuttonclick: (Hmtgfooterview *) Footerview;@end@interfaceHmtgfooterview:uiview//If a proxy uses a strong reference, a circular reference is generated, causing the controller and child views to be freed, causing a memory leak@property (nonatomic, weak)ID<HMTgFooterViewDelegate>Delegate;+(instancetype) Footerview;@end

hmtgfooterview.m

#import "HMTgFooterView.h"@interfaceHmtgfooterview () @property (weak, nonatomic) Iboutlet UIButton*Loadmorebutton, @property (weak, nonatomic) Iboutlet UIView*Tipsview;@end@implementationHmtgfooterview+(instancetype) footerview{return[[[NSBundle Mainbundle] loadnibnamed:@"Hmtgfooterview"Owner:nil Options:nil] lastobject];}-(ibaction) loadmore{NSLog (@"load More"); //1. Hide the buttonSelf.loadMoreButton.hidden =YES; //2. Display the prompt viewSelf.tipsView.hidden =NO; //delay Execution command, multithreaded gcd//future operation on delay, unified use of Dispatch_afterDispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (1.0* nsec_per_sec)), Dispatch_get_main_queue (), ^{        //code in the block code executes after 1 seconds//3. Load data (load from Web server, time required ...) //view is used to display the data, with the agent to achieve! //agent is used to listen, when something happens, notify the "agent"-"controller to" work "load data//3.1 Determine if the agent implements the Protocol method        if([Self.Delegaterespondstoselector: @selector (Tgfooterviewdiddownloadbuttonclick:)]) {[Self.DelegateTgfooterviewdiddownloadbuttonclick:self]; }                //4. Loading complete DataSelf.loadMoreButton.hidden =NO; Self.tipsView.hidden=YES; });}@end

iOS eighth day (1:uitableviewcontroller buy, click Bottom, xib load more, proxy mode)

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.