我们要学会封装,慢慢培养一种封装的思想
这样对我们的项目开发很有用处
项目的成败一个在于创意 另一个就是编码的质量
想在市场上的项目很多很多,但是真正能经历住考验的软件不多
因为项目开发中有个很重要的因素:
我们开发的项目如果不去用mvc 的思想去设计我们的代码
这对后期我们对项目的升级维护 带来很大的麻烦
一个项目的流产与否与这个因素密切相关
所以我们要养成一种良好的编码习惯
这对公司项目的开发有这很重要的意义
良好的代码风格与代码习惯方便了我们的开发
举个简单的例子
我们团队正在开发一个项目:项目周期有三个月
当过了一个月之后,有一位成员要离职
这对整个团队带来了很大的麻烦
这个人写的代码 都堆积在控制器中,几千行代码
别人去理解也要很长时间 去修改一下 几百个bug
那么有个很好的解决办法,我们可以使用mvc 的思想去开发一个项目
虽然说不是每个项目都用mvc 但这个解决了我们很大一部分困难
我们开发项目中,所有的模块都可以看成一个组件
使用的时候装在在控制器
不使用的时候我们删除
这样代码维护 与软件的升级是相当有好处的
今天这个小项目是承接上一篇来写的
将tableView 进行封装,这里只贴出来 tableView 的封装
大家可以看见控制器中的代码变的很少 很干净
这样开发程序 给我们带来很多好处,也给别人减少了麻烦
// // QHContentView.h // 广告轮播 // #import <UIKit/UIKit.h> @interface QHContentView : UIView //提供一个类方法 @property(nonatomic,strong)NSArray *subject; +(instancetype)contentView; @end
//
// QHContentView.m
// 广告轮播
//
#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;
//更新子控件UI 数据
[self.tableView reloadData];
}
//加载xib 文件就会自动调用
-(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 you perform custom drawing.
// An empty implementation adversely affects performance during 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
//懒加载 在从网络加载数据的时候我们 不能使用懒加载
// 会出现网络环境不好,加载延迟,所以不能使用
-(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 键值编码
QHSubject *subject = [QHSubject subjectWithDict:dict];
//建立数据模型
[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.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;
//
//
// [adsView setAdsViewDidSelected:^(QHAdsView * adsView, NSString * image, NSInteger index) {
// NSLog(@"%@&&&&&&",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 didReceiveMemoryWarning];
// // Dispose of any resources that can be recreated.
//}
//
//#pragma mark UITableViewDelegate
//
//-(NSInteger)tableView:(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 reuseIdentifier: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
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/u012701023/article/details/48034657