數據模型封裝演示樣例
阿新 • • 發佈:2017-06-06
pre objc lar ive 進步 div lec 推廣 leg
我們開發過程中 應該使用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
數據模型封裝演示樣例