在咱們生活中。瀏覽新聞的軟件到處可見,今日頭條,騰訊新聞咱們可以瀏覽到最新的諮詢數組
手機新聞時時在更新。而且內容格式不一,對於開發人員來講必須好好進行需求分析 mvc
今天講的這些問題適合剛開始學習的人學習app
這個項目使用了mvc 設計思路 將數據 view controller 進行分離 分別進行封裝 post
這個項目 有幾個模塊 有四種不一樣的cell 裏面的內容 圖片 也不一樣 ,咱們要對其進行一層層封裝 學習
好的代碼不是一下完畢的 咱們需要先將基本功能完畢,而後對咱們的代碼進行重構。優化 優化
這樣以來代碼的質量會提高,並且易於維護,添加了這款軟件的生命週期 atom
上主要代碼了:設計
// // 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
// // QHLargeCell.h #import <UIKit/UIKit.h> #import "QHnews.h" @interface QHLargeCell : UITableViewCell @property(nonatomic,strong)QHnews *news; +(id)largeCellWithTableView:(UITableView *)tableView; @end
// // QHLargeCell.m #import "QHLargeCell.h" @interface QHLargeCell() @property (weak, nonatomic) IBOutlet UILabel *titleLabel; @property (weak, nonatomic) IBOutlet UIImageView *pictureImageView; @property (weak, nonatomic) IBOutlet UILabel *sourceLabel; @property (weak, nonatomic) IBOutlet UILabel *timeLabel; @end @implementation QHLargeCell +(id)largeCellWithTableView:(UITableView *)tableView { NSString *name = NSStringFromClass([self class]); UINib *nib = [UINib nibWithNibName:name bundle:nil]; [tableView registerNib:nib forCellReuseIdentifier:name]; return [tableView dequeueReusableCellWithIdentifier:name]; } -(void)setNews:(QHnews *)news { _news = news; self.titleLabel.text = news.title; self.sourceLabel.text = news.source; self.timeLabel.text = news.time; self.pictureImageView.image = [UIImage imageNamed:news.picture]; // NSLog(@"%@---------------",news.picture); // NSLog(@"%@++++++",self.pictureImageView.image); } - (void)awakeFromNib { // Initialization code } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } @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