数据模型封装演示样例

我们开发过程中 应该使用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
时间: 2024-08-01 12:15:20

数据模型封装演示样例的相关文章

JDBC连接MySQL数据库及演示样例

JDBC是Sun公司制定的一个能够用Java语言连接数据库的技术. 一.JDBC基础知识         JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,能够为多种关系数据库提供统一訪问,它由一组用Java语言编写的类和接口组成.JDBC为数据库开发者提供了一个标准的API,据此能够构建更高级的工具和接口,使数据库开发者能够用纯 Java API 编写数据库应用程序,而且可跨平台执行,而且不受数据库供应商的限制.

java设计模式演示样例

创建模式 1.工厂方法模式(Factory Method)  将程序中创建对象的操作,单独出来处理,创建一个产品的工厂接口,把实际的工作转移到详细的子类.大大提高了系统扩展的柔性,接口的抽象化处理给相互依赖的对象创建提供了最好的抽象模式. public class TestFactoryMethod { public static void main(String[] args) { AnimalFactory af=new DogFactory(); Animal1 a=af.getAnima

Android平台调用Web Service:演示样例

近期在学习Android,随着移动设备的流行,当软件走上商业化的道路.为了争夺市场,肯定须要支持Android的,所以開始接触了Android,只是仅仅了解皮毛就好,由于我们要做管理者嘛.懂点Android.管理起来easy些. Android学起来也简单,封装的更好了,一个个的控件,像是又回到了VB的赶脚. 以下将通过一个演示样例解说怎样在Android平台调用Web Service. 我们使用互联网现成的Webservice.供查询手机号码归属地的Web service,它的WSDL为htt

kqueue演示样例

网络server通常都使用epoll进行异步IO处理,而开发人员通常使用mac,为了方便开发.我把自己的handy库移植到了mac平台上. 移植过程中,网上竟然没有搜到kqueue的使用样例.让我吃惊不已.为了让大家不用像我一样再次花费大力气搞定kqueue,我整理了一个简单清晰可执行的kqueue样例,供大家參考. kqueue一共同拥有几个函数: int kqueue(void); //相似epoll_create int kevent(int kq, const struct kevent

Lambda 表达式的演示样例-来源(MSDN)

本文演示怎样在你的程序中使用 lambda 表达式. 有关 lambda 表达式的概述.请參阅 C++ 中的 Lambda 表达式. 有关 lambda 表达式结构的具体信息,请參阅 Lambda 表达式语法. 本文内容 声明 Lambda 表达式 调用 Lambda 表达式 嵌套 Lambda 表达式 高阶 Lambda 函数 在函数中使用 Lambda 表达式 配合使用 Lambda 表达式和模板 处理异常 配合使用 Lambda 表达式和托管类型 声明 Lambda 表达式 演示样例 1

boost.python编译及演示样例

欢迎转载,转载请注明原文地址:http://blog.csdn.net/majianfei1023/article/details/46781581 linux编译boost的链接:http://blog.csdn.net/majianfei1023/article/details/46761029 昨天编译安装好boost,今天准备使用boost.python写个python调用c++代码的样例,结果踩了非常多坑. 首先贴上代码: 1.student.cpp,一个普通的c++类 #includ

10分钟理解Android数据库的创建与使用(附具体解释和演示样例代码)

1.Android数据库简单介绍. Android系统的framework层集成了Sqlite3数据库.我们知道Sqlite3是一种轻量级的高效存储的数据库. Sqlite数据库具有以下长处: (1)零配置,无需安装和配置: (2)储存在单一磁盘文件里的一个完整的数据库. (3)数据库文件能够在不同字节顺序的机器间自由共享: (4)支持数据大小至2TB: (5)足够小.全部源码大致3万行C代码.250KB: (6)比眼下流行的大多数数据库的操作要快. (7)开源. 2.Sqlite 基本操作语句

android listview综合使用演示样例_结合数据库操作和listitem单击长按等事件处理

本演示样例说明: 1.自己定义listview条目样式,自己定义listview显示列数的多少,灵活与数据库中字段绑定. 2.实现对DB的增删改查,而且操作后listview自己主动刷新. 3.响应用户操作点击事件,演示样例中展示单击时取出主键Id和其它内容. 4.响应用户操作长按事件,演示样例中展示长按时依据主键Id来编辑和删除数据. 5.表现层与数据处理层分开,不依赖于cursor(使用cursor不易表现和业务分离),支持接口编程. 6.使用数据库处理框架AHibernate灵活操作sql

最简单的视音频播放演示样例7:SDL2播放RGB/YUV

===================================================== 最简单的视音频播放演示样例系列文章列表: 最简单的视音频播放演示样例1:总述 最简单的视音频播放演示样例2:GDI播放YUV, RGB 最简单的视音频播放演示样例3:Direct3D播放YUV,RGB(通过Surface) 最简单的视音频播放演示样例4:Direct3D播放RGB(通过Texture) 最简单的视音频播放演示样例5:OpenGL播放RGB/YUV 最简单的视音频播放演示样例