We have to learn to encapsulate, to slowly develop a package of ideas
This is useful for our project development.
The success of the project is one of creativity, the other is the quality of coding
Want to be in the market a lot of projects, but really can experience the test of software is not much
Because there is a very important factor in the development of the project:
We develop projects that don't use MVC's ideas to design our code.
This has caused a lot of trouble for the upgrade and maintenance of the project later on.
Whether a project's miscarriage is closely related to this factor
So we have to develop a good coding habit.
This is very important for the development of the company's projects.
Good code style and code habits facilitate our development
To give a simple example
Our team is developing a project that has a three-month project cycle
After one months, one of the members is leaving.
It's a lot of trouble for the whole team.
This person writes code that is stacked in the controller, thousands of lines of code
It takes a long time for others to understand it. Hundreds of bugs
Well, there's a good solution, and we can use the idea of MVC to develop a project.
Although not every project uses MVC, but this solves a large part of our difficulties
In our development project, all modules can be viewed as a component
When used, it is installed in the controller
Do not use when we delete
This is quite beneficial for code maintenance and software upgrades.
Today this small project is to undertake the previous article to write
The TableView is encapsulated and is only affixed to the TableView package.
You can see that the code in the controller has changed very little, very clean.
So the development program gives us a lot of benefits, but also reduces the trouble to others
qhcontentview.h// AD Carousel//#import <UIKit/UIKit.h> @interface qhcontentview:uiview// Provides a class method @property (Nonatomic,strong) Nsarray *subject;+ (instancetype) Contentview; @end
qhcontentview.m//AD Carousel//#import "QHContentView.h" #import "QHSubjectCell.h" #import "QHAdsView.h" @interface Qhcontentview () <UITableViewDataSource,QHAdsViewDelegate,UITableViewDelegate> @property (weak, nonatomic) Iboutlet UITableView *tableview, @end @implementation qhcontentview+ (instancetype) contentview{return [[[NSBundle] Mainbundle]loadnibnamed:nsstringfromclass ([self class]) Owner:nil options:nil]lastobject];} -(void) Setsubject: (Nsarray *) subject{_subject = subject; Update child control UI data [Self.tableview Reloaddata];} The load Xib file is automatically called-(void) awakefromnib{qhadsview *adsview = [Qhadsview Adsview]; [Self.view Addsubview:adsview]; Self.tableView.tableHeaderView = Adsview; Self.tableView.delegate = self; Self.tableView.dataSource = self; Adsview.images = @[@ "ad_00", @ "ad_01", @ "ad_02", @ "ad_03", @ "ad_04"]; Adsview.delegate = self;} -(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{RetuRN Self.subject.count;} -(Nsinteger) Numberofsectionsintableview: (UITableView *) tableview{return 1;} -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{ Qhsubjectcell *cell = [Qhsubjectcell Subjectcellwithtableview:tableview]; Cell.subject = Self.subject[indexpath.row]; return cell;} -(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{return 100;} /*/Only override drawrect:if-perform custom drawing.//an empty implementation adversely affects performance Durin G animation.-(void) DrawRect: (cgrect) Rect {//Drawing code}*/@end
#import "ViewController.h" #import "QHAdsView.h" #import "QHSubject.h" #import "QHSubjectCell.h" #import " QHContentView.h "@interface Viewcontroller () <qhadsviewdelegate,uitableviewdatasource,uitableviewdelegate>@ Property (weak, nonatomic) Iboutlet UITableView *tableview; @property (Nonatomic,strong) Nsarray *plist; @property ( Nonatomic,strong) Nsarray *subject; @end @implementation viewcontroller//lazy loading when loading data from the network we can not use lazy loading//will appear bad network environment, load delay, So you cannot use-(Nsarray *) plist{if (_plist = = nil) {NSString *path = [[NSBundle mainbundle]pathforresource:@] Quanquan. Plist "Oftype:nil"; Nsarray *array = [Nsarray Arraywithcontentsoffile:path]; _plist = array; } return _plist;} -(Nsarray *) subject{if (_subject = = nil) {Nsarray *dicts = self.plist[1]; Nsmutablearray *OBJS = [Nsmutablearray array]; For (Nsdictionary *dict in dicts) {//KVC key value encoding qhsubject *subject = [Qhsubject subjectwithdic T:DICT]; Establishing a data model [OBJS Addobject:subject]; } _subject = Objs; } return _subject;} -(void) viewdidload {[Super viewdidload]; Qhcontentview *contentview = [Qhcontentview Contentview]; [Self.view Addsubview:contentview]; Contentview.subject = Self.subject; Qhadsview *adsview = [Qhadsview adsview];//////[self.view addsubview:adsview];//Self.tableView.tableHead Erview = adsview;////Self.tableView.delegate = self;////Self.tableView.dataSource = self;////////ADSVI Ew.images = @[@ "ad_00", @ "ad_01", @ "ad_02", @ "ad_03", @ "ad_04"];//adsview.delegate = self;//////[Adsview Setad sviewdidselected:^ (Qhadsview * adsview, NSString * image, Nsinteger index) {//NSLog (@ "%@&&&&&& amp; ", image);/}]; Do any additional setup after loading the view, typically from a nib.} -(void) adsviewdidselected: (Qhadsview *) Adsview andimage: (NSString *) image andindex: (nsinteger) index{//NSLog (@ "%@", Nsstringfromselector (_cmd)),//}//-(void) didreceivememorywarning {//[super Didreceivememorywarni ng];///Dispose of any resources the can be recreated.//}////#pragma mark uitableviewdelegate////-(Nsinteger) Tablevi EW: (UITableView *) TableView numberofrowsinsection: (nsinteger) section//{//return self.subject.count;//}////////-( UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath//{///*//static NSString *cellname = @ "cell";///UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellname];/ ///if (cell = = nil) {//cell = [[UITableViewCell alloc]initwithstyle:uitableviewcellstylesubtitle Reuseiden tifier:cellname];//}//Cell.textLabel.text = @ "text";//return cell;//*/////Qhsubjectcell *cell = [ Qhsubjectcell subjectcellwithtableview:tableview];////Cell.subject = self.subject[indexpath.row];////NSLog (@ "%@", cell.subject);/// Return cell;//}//-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath//{/ /Return 100;//} @end
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Gourmet House-ios Project source code-TableView Package