Data Model Encapsulation Example

Source: Internet
Author: User

We should use the MVC development model in our development process

Before I talked about MVC is not just a design pattern, here is not much to explain, before you can see

Data encapsulation makes our basic skills widely used in development

We must attach importance to

Here in order to give beginners a learning platform for the knowledge of the explanation

I'm going to do it in a gradual way--I hope it helps.

Only sharing will make progress.

  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) {//package model qhnews *news = [Qhnews newswithdict:dic];                    [Objs addobject:news];    } _newses = Objs; } return _newses;} #pragma mark Uitableviewdatasource Proxy Method-(Nsinteger) Numberofsectionsintableview: (UITableView *) tableview{return 1;} -(Nsinteger) TableView: (UITableView *) TableView numbErofrowsinsection: (Nsinteger) section{return self.newses.count;} -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{UITABLEVIEWC    ell * 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:tablev        Iew];        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 the can be recreated.} @end 

  qhnews.h//Building a data Model #import <Foundation/Foundation.h> @interface qhnews:nsobject/** *  News items Category */@ Property (nonatomic,copy) nsstring *category;/** *  record image array */@property (Nonatomic,strong) nsarray *pics;/** *  Data source */@property (nonatomic,copy) nsstring *source;/** *  release time */@property (nonatomic,copy) nsstring *time;/** *  Title */@property (nonatomic,copy) nsstring *title;/** *  for recording a single picture */@property (nonatomic,copy) nsstring *picture;/** *  name used to record promotional software */@property (nonatomic,copy) nsstring *appname;/** *  Promotion software Picture */@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,_sour Ce,_title];} @end



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Data Model Encapsulation Example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.