IOS to Viewcontroller lighten the UITableView

Source: Internet
Author: User

Today read some blog posts to share how to give Viewcontroller the problem of slimming, one of which is tableview.

Indeed, with the product iteration, VC inside may become more and more bloated, sometimes really need to do a good slimming. Maybe the reference blog to explain more emphasis on the method of reuse, in fact, this is really very flexible problem, sometimes tableview list is the same specification is more suitable for reusable methods to achieve, Sometimes it needs to be customized individually, and a tableview may have more than two to three cell types. So for the latter. I also modeled aUITableViewDataSource UITableViewDelegate 都提取出来单独做一个对象做处理.

Requirement: A tableview inside the current element is a row banner a line of time several rows list element composition

Effect:

Sectiondemo as follows:

////Viewcontroller Burden. h//chain-type DSL learning////Created by HF on 17/3/8.//Copyright 2017 Hf-liqun. All rights reserved.//#import<UIKit/UIKit.h>@interface viewcontroller__: Uiviewcontroller@property (nonatomic, strong) UITableView*TableView; @end////Viewcontroller burden. M//chain-type DSL learning////Created by HF on 17/3/8.//Copyright 2017 Hf-liqun. All rights reserved.//#import"Viewcontroller Burden. h"#import"HomeBannerCell.h"#import"HomeTableViewDataSource.h"@interface viewcontroller__ () @property (nonatomic, strong) Hometableviewdatasource *Tabbleviewdatasource, @end @implementation viewcontroller__- (void) viewdidload {[Super viewdidload];    [Self.view AddSubview:self.tableView]; [Self.tableview mas_makeconstraints:^ (Masconstraintmaker *Make )        {Make.bottom.equalTo (Self.view);    Make.left.right.top.equalTo (Self.view);        }]; //Create an instance class Tabbleviewdatasource//incoming data This can be handled flexibly according to the actual situationSelf.tabbleviewdatasource =[[hometableviewdatasource alloc] initwithitems:@[@"Good",@"Learning",@"every day",@"Upward"] ]; //set TableView two agentsSelf.tableview.DataSource=Self.tabbleviewdatasource; Self.tableview.Delegate=Self.tabbleviewdatasource; //Click Trigger response per cell rowTableviewcellbanneraction bannneraction = ^(id sender) {NSLog (@"%@", sender);    }; Tableviewcelldailyaction dailyaction= ^(id sender) {NSLog (@"%@", sender);    }; Tableviewcelllistaction listaction= ^(id sender) {NSLog (@"%@", sender);    }; Self.tabbleViewDataSource.banneryAction=bannneraction; Self.tabbleViewDataSource.dailyAction=dailyaction; Self.tabbleViewDataSource.listAction=listaction; }- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}-(UITableView *) tableview{if(!_tableview) {_tableview=[[UITableView alloc] Initwithframe:cgrectzero Style:uitableviewstyleplain]; _tableview.backgroundcolor=[Uicolor Clearcolor]; //_tableview.datasource = self; //_tableview.delegate = self; //_tableview.separatorstyle = Uitableviewcellseparatorstylenone;[Self.tableview Registerclass:[homebannercellclass] Forcellreuseidentifier:khomebannercellid]; [Self.tableview Registerclass:[uitableviewcellclass] Forcellreuseidentifier:@"Tableviewcell"]; }    return_tableview;} @end

Next is the specific implementation of the Hometableviewdatasource class

////HomeTableViewDataSource.h//chain-type DSL learning////Created by HF on 17/3/8.//Copyright 2017 Hf-liqun. All rights reserved.//#import<Foundation/Foundation.h>typedefvoid(^tableviewcellbanneraction) (ID sender); typedefvoid(^tableviewcelldailyaction) (ID sender); typedefvoid(^tableviewcelllistaction) (id sender); @interface Hometableviewdatasource:nsobject<uitableviewdelegate, uitableviewdatasource>
@property (nonatomic, copy) tableviewcellbanneraction banneryaction; @property (nonatomic, copy) Tableviewcelldailyaction dailyaction; @property (nonatomic, copy) tableviewcelllistaction listaction;/** Incoming data source @param anitems anitems @return anitems*/-(ID) Initwithitems: (Nsarray *) Anitems; @end////HOMETABLEVIEWDATASOURCE.M//chain-type DSL learning////Created by HF on 17/3/8.//Copyright 2017 Hf-liqun. All rights reserved.//#import"HomeTableViewDataSource.h"#import"HomeBannerCell.h"typedef ns_enum (Nsuinteger, Homesectiontype) {homesectiontypebanner,//BannerHomesectiontypedaily,//DateHomesectiontypelist//List}; @interface Hometableviewdatasource () {Nsmutablearray*Sectioninfo;} @property (nonatomic, strong) Nsarray*items, @end @implementation hometableviewdatasource-(ID) init{returnNil;}-(ID) Initwithitems: (Nsarray *) anitems{ Self=[Super Init]; if(self) {sectioninfo=[Nsmutablearray array]; [Sectioninfo addobject:@ (Homesectiontypebanner)]; [Sectioninfo addobject:@ (homesectiontypedaily)]; if(Anitems.count >0) {[Sectioninfo addobject:@ (Homesectiontypelist)];//List Lists} self.items=Anitems; } returnSelf ;}-(ID) Itematindexpath: (Nsindexpath *) indexpath{returnself.items[(Nsuinteger) indexpath.row];}#pragmaMark Uitableviewdatasource-(Nsinteger) Numberofsectionsintableview: (UITableView *) tableview{returnSectioninfo.count;}-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{if(Homesectiontypebanner = =[sectioninfo[section] integervalue]) { return 1; } if(homesectiontypedaily = =[sectioninfo[section] integervalue]) { return 1; } if(Homesectiontypelist = =[sectioninfo[section] integervalue]) { returnSelf.items.count; } return 0;}-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{nsinteger section=indexpath.section; if(Homesectiontypebanner = =[sectioninfo[section] integervalue]) {Homebannercell*cell =[TableView dequeuereusablecellwithidentifier:khomebannercellid Forindexpath:indexpath]; returncell; } if(homesectiontypedaily = =[sectioninfo[section] integervalue]) {UITableViewCell*cell = [TableView dequeuereusablecellwithidentifier:@"Tableviewcell"Forindexpath:indexpath]; Cell.textLabel.text= [NSString stringWithFormat:@"%@", [NSDate Date]]; Cell.textLabel.font= [Uifont boldsystemfontofsize: the]; Cell.textLabel.textAlignment=Nstextalignmentcenter; returncell; } if(Homesectiontypelist = =[sectioninfo[section] integervalue]) {UITableViewCell*cell = [TableView dequeuereusablecellwithidentifier:@"Tableviewcell"Forindexpath:indexpath]; ID Item=[self itematindexpath:indexpath]; Cell.textLabel.text= [NSString stringWithFormat:@"%@", item]; returncell; } return[UITableViewCellNew];}#pragmaMark Uitableviewdelegate-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{nsinteger section=indexpath.section; if(Homesectiontypebanner = =[sectioninfo[section] integervalue]) { return[Homebannercell Getcellheight]; } if(homesectiontypedaily = =[sectioninfo[section] integervalue]) { return -; } if(Homesectiontypelist = =[sectioninfo[section] integervalue]) { return -; } return 0;}- (void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{nsinteger section=indexpath.section; if(Homesectiontypebanner = =[sectioninfo[section] integervalue]) { if(self.banneryaction) {self.banneryaction (@"clicked on Banner"); } } if(homesectiontypedaily = =[sectioninfo[section] integervalue]) { if(self.dailyaction) {self.dailyaction (@"clicked on the date"); } } if(Homesectiontypelist = =[sectioninfo[section] integervalue]) { if(self.listaction) {ID Item=[self itematindexpath:indexpath]; Self.listaction ([NSString stringWithFormat:@"Click the list unit%@", item]); } }} @end

Reference:

1.https://objccn.io/issue-1-1/

IOS to Viewcontroller lighten the 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.