One of the Viewcontroller slimming (reproduced)

Source: Internet
Author: User

In an iOS project, Viewcontroller is usually the largest file and contains many unnecessary code, with the lowest reuse rate.

We can try to give viewcontroller weight, make him look not so bloated.

Today's talk is about, Uitableviewdatasource.

UITableView may be the most commonly used in writing projects in the component, use it to implement its proxy protocol and data source method, each time is a thing method controller, look uncomfortable, we can give him a thin body. For example, to do so.

Because DataSource is basically around the array to do something, more specifically around the Viewcontroller data array to do something, we can pull it out, separate in a class, we use a block to set the cell, You can also use delegate to do this, which is entirely up to you.

Below the code, for a chestnut ~

1 //2 //ArrayDataSource.h3 //lrdbasicframework4 //5 //Created by the dancer on the keyboard on 5/21/16.6 //copyright©2016 The dancers on the keyboard. All rights reserved.7 //encapsulates a DataSource class that can make controller more lightweight8  9 #import<Foundation/Foundation.h>Ten   Onetypedefvoid(^tableviewcellconfigureblock) (IDCellIDitem); A   - @interfaceArraydatasource:nsobject <UITableViewDataSource> -   the- (ID) Initwithitems: (Nsarray *) Items -Cellidentifier: (NSString *) Identifier - Configureblock: (tableviewcellconfigureblock) Configureblock; -   +- (ID) Itematindexpath: (Nsindexpath *) Indexpath; -   + @end
1 //2 //ARRAYDATASOURCE.M3 //lrdbasicframework4 //5 //Created by the dancer on the keyboard on 5/21/16.6 //copyright©2016 The dancers on the keyboard. All rights reserved.7 //8  9 #import "ArrayDataSource.h"Ten   One @interfaceArraydatasource () A   -@property (nonatomic, strong) Nsarray *items; -@property (nonatomic, copy) NSString *identifier; the @property (nonatomic, copy) Tableviewcellconfigureblock Configureblock; -   - @end -   + @implementationArraydatasource -   +-(instancetype) init { A     returnNil; at } -   -- (ID) Initwithitems: (Nsarray *) Items Cellidentifier: (NSString *) identifier Configureblock: (Tableviewcellconfigureblock) Configureblock { -Self =[Super init]; -     if(self) { -Self.items =items; inSelf.identifier =identifier; -Self.configureblock =Configureblock; to     } +     returnSelf ; - } the   *- (ID) Itematindexpath: (Nsindexpath *) Indexpath { $     returnself.items[(Nsuinteger) indexpath.row];Panax Notoginseng } -   the #pragmaMark-datasource +-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section { A     returnSelf.items.count; the } +   --(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath { $UITableViewCell *cell =[TableView dequeueReusableCellWithIdentifier:self.identifier Forindexpath:indexpath]; $     IDitem =[self itematindexpath:indexpath]; - Self.configureblock (cell, item); -     returncell; the } - @end

And then we can use it in Viewcontroller.

1 //2 //DEMOROOTCONTROLLER.M3 //lrdbasicframework4 //5 //Created by the dancer on the keyboard on 5/21/16.6 //copyright©2016 The dancers on the keyboard. All rights reserved.7 //8  9 #import "DemoRootController.h"Ten #import "ArrayDataSource.h" One #import "DemoFrameController.h" A #import "DemoFouctionController.h" - #import "DemoDIYController.h" - #import "DemoCommUtils.h" the   - StaticNSString *identifier =@"Menu_cell"; -   - @interfaceDemorootcontroller () +   -@property (nonatomic, strong) Arraydatasource *Arraydatasource; +   A @end at   - @implementationDemorootcontroller -   -- (void) Viewdidload { - [Super Viewdidload]; -Self.title =@"Demo"; in     //Initialize - [self initui]; to } +   -- (void) Initui { theNsarray *items = @[@"Custom Controls",@"Functional Expansion",@"Common tool Classes",@"Basic Framework"]; *Self.arraydatasource = [[Arraydatasource alloc] Initwithitems:items cellidentifier:identifier configureBlock:^ ( UITableViewCell *cell, NSString *Item) { $Cell.accessorytype =Uitableviewcellaccessorydisclosureindicator;Panax NotoginsengCell.textLabel.text =item; -     }]; theSelf.tableView.dataSource =Self.arraydatasource; +[Self.tableview Registerclass:[uitableviewcellclass] forcellreuseidentifier:identifier]; A } the   + #pragmaMark-delegate -- (void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath { $     if(Indexpath.row = =0) { $         //Custom Controls -Demodiycontroller *VC =[[Demodiycontroller alloc] initwithstyle:uitableviewstylegrouped]; -Vc.title =[Self.arraydatasource Itematindexpath:indexpath]; the [Self.navigationcontroller PUSHVIEWCONTROLLER:VC animated:yes]; -}Else if(Indexpath.row = =1) {Wuyi         //Functional Expansion theDemofouctioncontroller *VC =[[Demofouctioncontroller alloc] initwithstyle:uitableviewstylegrouped]; -Vc.title =[Self.arraydatasource Itematindexpath:indexpath]; Wu [Self.navigationcontroller PUSHVIEWCONTROLLER:VC animated:yes]; -}Else if(Indexpath.row = =2) { About         //Common tool Classes $Democommutils *VC =[[Democommutils alloc] initwithstyle:uitableviewstylegrouped]; -Vc.title =[Self.arraydatasource Itematindexpath:indexpath]; - [Self.navigationcontroller PUSHVIEWCONTROLLER:VC animated:yes]; -}Else if(Indexpath.row = =3) { A         //Basic Framework +Demoframecontroller *VC =[[Demoframecontroller alloc] initwithstyle:uitableviewstylegrouped]; theVc.title =[Self.arraydatasource Itematindexpath:indexpath]; - [Self.navigationcontroller PUSHVIEWCONTROLLER:VC animated:yes]; $     } the [TableView Deselectrowatindexpath:indexpath animated:yes]; the } the   the @end

Does it look like Viewcontroller has lost a lot of weight??

Transferred from: http://www.lrdup.net/archives/1046

One of the Viewcontroller slimming (reproduced)

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.