您的位置:首页 > 其它

新闻头条-软件开发

2015-08-19 00:11 316 查看
在我们生活中,浏览新闻的软件处处可见,今日头条,腾讯新闻我们可以浏览到最新的咨询

手机新闻时时在更新,而且内容格式不一,对于开发者来说必须好好进行需求分析

今天讲的这些问题适合初学者学习

这个项目使用了mvc 设计思路 将数据 view controller 进行分离 分别进行封装

这个项目 有几个模块 有四种不同的cell 里面的内容 图片 也不同 ,我们要对其进行一层层封装

好的代码不是一下完成的 我们需要先将基本功能完成,然后对我们的代码进行重构,优化

这样以来代码的质量会提升,并且易于维护,增加了这款软件的生命周期

上主要代码了:

//
//  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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: