首页 > 代码库 > 数据模型封装演示样例

数据模型封装演示样例

我们开发过程中 应该使用mvc 的开发模式 

之前有讲过 mvc 不不过设计模式,这里不多解说了,之前的能够看看

数据封装使我们的基本功。在开发其中普遍的使用

我们必须重视 

这里为了给刚開始学习的人一个学习的平台。对于知识的解说

我会以一种循序渐进的方式-希望对大家有所帮助

仅仅有分享才会进步

//
//  QHViewController.h


#import <UIKit/UIKit.h>

@interface QHViewController : UIViewController

@end

//
//  QHViewController.m


#import "QHViewController.h"
#import "QHnews.h"
#import "NSArray+Ext.h"
#import "QHLargeCell.h"
#import "QHListCell.h"
#import "QHOriginCell.h"
#import "QHAppCell.h"


@interface QHViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)NSArray *newses;

@end


@implementation QHViewController

-(NSArray *)newses
{
    if (_newses == nil) {
        NSString *path = [[NSBundle mainBundle]pathForResource:@"news.plist" ofType:nil];
        NSArray *array = [NSArray arrayWithContentsOfFile:path];
        
        NSMutableArray *objs = [NSMutableArray array];
        for(NSDictionary *dic in array)
        {
            //封装模型 
            QHnews *news = [QHnews newsWithDict:dic];
            [objs addObject:news];
            
        }
        
        _newses = objs;
    }
    return _newses;
}

#pragma mark UITableViewDataSource 代理方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.newses.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    

    UITableViewCell * cell = [[UITableViewCell alloc]init];
    /*
    if (cell == nil) {
        cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    }
    cell.textLabel.text = @"test";
    */
    
    QHnews *news = self.newses[indexPath.row];
    if ([news.category isEqualToString:@"large"]) {
        QHLargeCell *cell = [QHLargeCell largeCellWithTableView:tableView];
        cell.news = self.newses[indexPath.row];
        return cell;
    }
    if ([news.category isEqualToString:@"list"]) {
        QHListCell *cell = [QHListCell listCellWithTableView:tableView];
        cell.news = self.newses[indexPath.row];
        return cell;
    }
    if ([news.category isEqualToString:@"origin"]) {
        QHOriginCell *cell = [QHOriginCell originWithTableView:tableView];
        cell.news = self.newses[indexPath.row];
        return cell;
    }
    if([news.category isEqualToString:@"app"])
    {
        QHAppCell *cell = [QHAppCell appWithTableView:tableView];
        cell.news = self.newses[indexPath.row];
        return cell;
    }
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    QHnews *news = self.newses[indexPath.row];
    
    if([news.category isEqualToString:@"large"])
    {
        return 150;
    }
    if ([news.category isEqualToString:@"list"]) {
        return 150;
        
    }
    if([news.category isEqualToString:@"origin"])
    {
        return 100;
    }
    if ([news.category isEqualToString:@"app"]) {
        return 120;
    }
    return 100;
}
-(BOOL)prefersStatusBarHidden
{
    return YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //NSLog(@"%@",self.newses);
 
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

//
//  QHnews.h

//建立数据模型

#import <Foundation/Foundation.h>

@interface QHnews : NSObject



/**
 *  新闻条目分类
 */
@property(nonatomic,copy)NSString *category;
/**
 *  记录图片数组
 */
@property(nonatomic,strong)NSArray *pics;

/**
 *  数据来源
 */

@property(nonatomic,copy)NSString *source;
/**
 *  公布时间
 */

@property(nonatomic,copy)NSString *time;

/**
 *  标题
 */

@property(nonatomic,copy)NSString *title;

/**
 *  用来记录单张图片
 */
@property(nonatomic,copy)NSString *picture;

/**
 *  用来记录推广软件的名称
 */
@property(nonatomic,copy)NSString *appname;

/**
 *  推广软件图片
 */
@property(nonatomic,copy)NSString *icon;

@property(nonatomic,strong)QHnews *news;
+(id)newsWithDict:(NSDictionary *)dict;
-(id)initWithDict:(NSDictionary *)dict;
@end

//
//  QHnews.m

#import "QHnews.h"

@implementation QHnews
+(id)newsWithDict:(NSDictionary *)dict
{
    return [[self alloc]initWithDict:dict];
}
-(id)initWithDict:(NSDictionary *)dict
{
    if (self == [super init]) {
        [self setValuesForKeysWithDictionary:dict];
    }
    
    return self;
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"catefory = %@,source = %@,title = %@", _category,_source,_title];
}
@end



数据模型封装演示样例