MVC(模型-視圖-控制器)的實現

MVC(模型-視圖-控制器)是一種設計思想,貫穿於整個ios開發當中,當積累了一定的開發經驗時,你就能深刻的領會MVC(模型-視圖-控制器)當中的好處和真正含義。

MVC(模型-視圖-控制器)主要有三個角色:

》M:模型數據(Model)

》V:視圖或界面(View)

》C:控制器(Control)

MVC(模型-視圖-控制器)的明顯特徵:

》View上顯示什麼內容完全取決於Model;

》一旦Model數據發生改變,View的顯示狀態也會跟着發生變化;

》Control負責初始化Model,並且將Model傳遞給View去解析和展示;

假設利用這一思想來重構代碼,將plist文件中的字典轉化成模型,代碼如下:

code:

首先創建一個class:ZHLCosmeticsGroup

ZHLCosmeticsGroup.h:

#import <Foundation/Foundation.h>

@interface ZHLCosmeticsGroup : NSObject
/**
 *  頭部標題
 */
@property (nonatomic, copy) NSString *title;

/**
 *  尾部標題(描述)
 */
@property (nonatomic, copy) NSString *description;

/**
 *  這組的所有化妝品牌(字符串)
 */
@property (nonatomic, strong) NSArray *Cosmetics;
@end


ViewCotroller.m文件:

#import <Foundation/Foundation.h>

@interface ZHLCosmeticsGroup : NSObject
#import "ViewController.h"
#import "ZHLCosmeticsGroup.h"

@interface ViewController () <UITableViewDataSource>


@property (nonatomic, strong) NSArray *CosmeticsGroups;
@end

@implementation ZHLViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 設置數據源
    self.tableView.dataSource = self;
}

// 隱藏狀態欄
- (BOOL)prefersStatusBarHidden
{
    return YES;
}

- (NSArray *)CosmeticsGroups
{
    if (_CosmeticsGroups == nil) {
        // 初始化
        // 法國品牌
        ZHLCosmeticsGroup *cg1 = [[ZHLCosmeticsGroup alloc] init];
        cg1.title = @"法國品牌";
        cg1.description = @"都是女生想要的";
        cg1.Cosmetics = @[@"迪奧", @"蘭蔻", @"薇姿", @"香奈兒"];
        
        // 日韓品牌
        ZHLCosmeticsGroup *cg2 = [[ZHLCosmeticsGroup alloc] init];
        cg2.title = @"日韓品牌";
        cg2.description = @"韓妝棒棒噠";
        cg2.Cosmetics = @[@"夢妝",@"whoo後" @"SK-II", @"植村秀", @"資生堂",@"Sensai"];
        
        // 歐系品牌
        ZHLCosmeticsGroup *cg3 = [[ZHLCosmeticsGroup alloc] init];
        cg3.title = @"歐美品牌";
        cg3.description = @"都是大牌";
        cg3.Cosmetics = @[@"雅詩蘭黛", @"倩碧"];
        
        _CosmeticsGroups = @[cg1, cg2, cg3];
    }
    return _CosmeticsGroups;
}

#pragma mark - 數據源方法
/**
 *  一共有多少組數據
 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.CosmeticsGroups.count;
}

/**
 *  第section組有多少行
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // 取得第section組對應的模型
    ZHLCosmeticsGroup *cg = self.CosmeticsGroups[section];
    
    return cg.Cosmetics.count;
}

/**
 *  每一行顯示怎樣的內容(cell)
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    
    // 取出第indexPath.section組對應的模型
    ZHLCosmeticsGroup *cg = self.CosmeticsGroups[indexPath.section];
    
    // 取車第indexPath.row這行對應的品牌名稱
    NSString *cos = cg.Cosmetics[indexPath.row];
    
    // 設置cell顯示的文字
    cell.textLabel.text = cos;
    
    return cell;
}

/**
 *  第section組顯示怎樣的頭部標題
 */
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    ZHLCosmeticsGroup *cg = self.CosmeticsGroups[section];
    
    return cg.title;
}

/**
 *  第section組顯示怎樣的尾部標題
 */
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    ZHLCosmeticsGroup *cg = self.CosmeticsGroups[section];
    
    return cg.description;
}

@end




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章