上一節(jié)主要是介紹了Django框架和對(duì)比我自己的架構(gòu)。這節(jié)就在我之前的基礎(chǔ)上復(fù)雜化一點(diǎn)。
這里我做了兩件事,抽取UITableView將它放入一個(gè)單獨(dú)的UIView中,在一個(gè)就是進(jìn)一步講解我們的主題。
抽取UITableView其實(shí)這是我們上一節(jié)該做的事情,因?yàn)槲覟榱斯?jié)約時(shí)間,我就沒有做抽取,這個(gè)過程是比較簡(jiǎn)單的。下面就是代碼:
TabelView.h
@interface TabelView : UIView
@property (nonatomic, strong) NSMutableArray *modelArray;
+ (instancetype)initAddTabelView;
@end
TabelView.m
+ (instancetype)initAddTabelView {
return [[TabelView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, UI_SCREEN_HEIGHT)];
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self initView];
}
return self;
}
- (void)setModelArray:(NSMutableArray *)modelArray {
_modelArray = modelArray;
}
- (void)initView {
UITableView *tabelView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, UI_SCREEN_HEIGHT) style:UITableViewStylePlain];
tabelView.delegate = self;
tabelView.dataSource = self;
[self addSubview:tabelView];
}
- (NSInteger)numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _modelArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
HomeTableViewCell *cell = [HomeTableViewCell homeTableViewCell:tableView];
cell.dataModel = _modelArray[indexPath.row];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
DataModel *data = _modelArray[indexPath.row];
if ([data.imageName isEqualToString:@""]) {
return 205;
} else{
return 100;
}
}
就是創(chuàng)建一個(gè)UIView將代碼搬過來就可以了。
下面就是具體講到我們這個(gè)主題了。我這里做一個(gè)假設(shè),我們要對(duì)樣式做一個(gè)添加,在添加一些數(shù)據(jù)。那看看我們這種架構(gòu)是如何做的。
因?yàn)槲疫@里是用的純代碼來創(chuàng)建cell,所以cell上要顯示的控件必須是在創(chuàng)建cell時(shí)就要實(shí)例化的,如果你不在這一過程中進(jìn)行實(shí)例化,那將會(huì)出現(xiàn)混亂。
下面就是代碼:
//在view初始化控件,(這里就如同web開發(fā)中的thml頁面,也就是web中mvc中的view)
- (void)initView {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, image_HW, image_HW)];
imageView.hidden = YES;
[self addSubview:_imageViews = imageView];
UILabel *nameLbl = [[UILabel alloc] initWithFrame:CGRectMake(100, 10, 100, 40)];
nameLbl.hidden = YES;
[self addSubview:_nameLbl = nameLbl];
UILabel *contentLbl = [[UILabel alloc] initWithFrame:CGRectMake(100, 55, UI_SCREEN_WIDTH - image_HW -10, image_HW - 55)];
contentLbl.hidden = YES;
[self addSubview:_contentLbl = contentLbl];
//樣式二
UILabel *titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, image_HW, 20)];
titleLbl.hidden = YES;
[self addSubview:_titleLbl = titleLbl];
UIImageView *imageSituation = [[UIImageView alloc] initWithFrame:CGRectMake(10, 30, UI_SCREEN_WIDTH - 20, 155)];
imageSituation.hidden = YES;
[self addSubview:_imageSituation = imageSituation];
UILabel *coordinateLbl = [[UILabel alloc] initWithFrame:CGRectMake(UI_SCREEN_WIDTH - 100, View_H - 15, 90, 15)];
coordinateLbl.textAlignment = NSTextAlignmentRight;
coordinateLbl.hidden = YES;
[self addSubview:_coordinateLbl = coordinateLbl];
}
在上一個(gè)基礎(chǔ)上添加了樣式二。
數(shù)據(jù)的添加:
//這里就是這個(gè)頁面的數(shù)據(jù)導(dǎo)入
- (void)initData:(DataModel *)dataModel {
_imageViews.hidden = NO;
_nameLbl.hidden = NO;
_contentLbl.hidden = NO;
_imageViews.image = [UIImage imageNamed:dataModel.imageName];
_nameLbl.text = dataModel.textLbl;
_contentLbl.text = dataModel.contentLbl;
}
//樣式二傳進(jìn)來的數(shù)據(jù)
- (void)initStyleData:(DataModel *)dataModel {
_titleLbl.hidden = NO;
_coordinateLbl.hidden = NO;
_imageSituation.hidden = NO;
_titleLbl.text = dataModel.titleLbl;
_imageSituation.image = [UIImage imageNamed:dataModel.imageSituation];
_coordinateLbl.text = dataModel.coordinateLbl;
}
是頁面上的操作都是view實(shí)現(xiàn)的,看著明了清晰。
view上的邏輯處理:
//數(shù)據(jù)邏輯(對(duì)應(yīng)的給每個(gè)view數(shù)據(jù),邏輯在這里處理)
- (void)setDataModel:(DataModel *)dataModel {
_dataModel = dataModel;
if ([_dataModel.imageName isEqualToString:@""]){
[_styleView initStyleData:_dataModel];
} else {
[_styleView initData:_dataModel];
}
}
好了所有的代碼都已添加完畢,看著是不是簡(jiǎn)單明了,就改了幾個(gè)地方,Controller就不要改動(dòng)。在StyleView中將所有的控件都進(jìn)行初始化這是一個(gè)必須的過程,不然就會(huì)導(dǎo)致亂序。在這里最重要就是HomeTableViewCell中的數(shù)據(jù)邏輯判斷了,這里不出錯(cuò)的話這個(gè)代碼都沒有問題了啊。是不是很簡(jiǎn)單啊,都是在模塊上添加代碼就可以了。
這是代碼地址:https://github.com/tangyi1234/mvcFramework1