码迷,mamicode.com
首页 > 移动开发 > 详细

IOS项目之 爱限免一

时间:2015-04-08 22:54:37      阅读:887      评论:0      收藏:0      [点我收藏+]

标签:

项目创建

1、获取项目所需的相关素材,界面效果图,界面的素材,以及拿到网络接口等。

技术分享

2、创建项目,项目中创建一些逻辑文件夹,用来存放相应的文件,清晰的展示

  引入AFNetworking;

3、各个ViewController的设计,设计多个界面分别展示。

#import "RootViewController.h"
#import "ApplistViewController.h"
#import "HotViewController.h"
#import "ScaleViewController.h"
#import "TopicViewController.h"
#import "FreeViewController.h"
#import "LimitViewController.h"

4.创建RootViewController的导航栏以及标签栏(此过程中进行了封装)

      4.1 继承UIImageView 的类别创建,便于后期创建多个button,或者多个Label

//添加系统按钮
-(UIButton *) addSystemButtonWithFrame: (CGRect )frame title:(NSString *)title action:(void (^)(UIButton *button))action{
    JayzyButton *button =[JayzyButton buttonWithType:UIButtonTypeSystem];
    button.frame =frame;
    [button setTitle:title forState:UIControlStateReserved];
    
    button.action =action;
    [self addSubview:button];
    return button;
}
//添加图片按钮
-(UIButton *) addImageButtonWithFrame: (CGRect )frame title:(NSString *)title background:(NSString *) background action:(void (^)(UIButton *button))action{

    JayzyButton *button =[JayzyButton buttonWithType:UIButtonTypeSystem];
    button.frame =frame;
    [button setTitle:title forState:UIControlStateReserved];
    [button setBackgroundImage:[UIImage imageNamed:background] forState:UIControlStateNormal];
    button.action =action;
    [self addSubview:button];
    return button;
    
}

//添加imageView
-(UIImageView*) addImageViewWithFrame: (CGRect )frame image:(NSString *)image{
    UIImageView *imageView =[[UIImageView alloc] initWithFrame:frame];
    imageView.image =[UIImage imageNamed:image];
    imageView.userInteractionEnabled =YES;
    [self addSubview:imageView];
    return imageView;

}
//添加标签
-(UILabel *) addLabelWithFrame: (CGRect )frame text:(NSString *)text{
    UILabel *label =[[UILabel alloc] initWithFrame:frame];
    label.text =text;
    [self addSubview:label];
    return label;
}

   4.2 需要创建5个视图由标签控制器控制,统一进行封装,传入类别,标签Title,标签背景图片

//快速添加一个试图控制器
-(UIViewController *) addViewController:(Class)cls title :(NSString *)title image: (NSString *) image
{
 //cls 传入界面所对应的类
    UIViewController *vc =[[cls alloc] init];
    vc.title =title;
    UINavigationController *nc =[[UINavigationController alloc] initWithRootViewController:vc];
    nc.tabBarItem.image =[UIImage imageNamed:image];
    
    //添加到标签中
    NSMutableArray *marr =[[NSMutableArray alloc] initWithArray:self.viewControllers];
    [marr addObject:nc];
    self.viewControllers =marr;
//返回创建的试图控制器
    return vc;

}

  4.3 开始设置首页导航栏的左右按钮

  [self configNavigationButton];

#pragma mark ------配置导航条
-(void)configNavigationButton
{
//要创建多个button的时候,要设置多个视图
    UIButton *leftButton =[UIButton buttonWithType:UIButtonTypeSystem];
    leftButton.frame =CGRectMake(0, 0, 45, 30);
    [leftButton setTitle:@"分类" forState:UIControlStateNormal];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"buttonbar_action.png"] forState:UIControlStateNormal];
[leftButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    leftButton.titleLabel.font =[UIFont systemFontOfSize:14.0f];
    [leftButton addTarget:self action:@selector(dealleftClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem =[[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem =leftItem;
    //添加右导航
    UIButton *rightButton =[UIButton buttonWithType:UIButtonTypeSystem];
    rightButton.frame =CGRectMake(0, 0, 45, 30);
    [rightButton setTitle:@"设置" forState:UIControlStateNormal];
    [rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [rightButton setBackgroundImage:[UIImage imageNamed:@"buttonbar_action.png"] forState:UIControlStateNormal];
    rightButton.titleLabel.font =[UIFont systemFontOfSize:14.0f];
    [rightButton addTarget:self action:@selector(dealrightClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem =[[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem =rightItem;
}

4.4下载数据进行解析,配置页面的UITableView

_dataArray=[[NSMutableArray alloc] init];
    _page =1;
    _categoryId =@"";//URL接口中" "表示全部
    [self startDownloadData];
============ ===============
-(void)startDownloadData
{
    //下载数据,JSON的解析
    NSString *urlString =[NSString stringWithFormat:LIMIT_URL,_page,_categoryId];
    AFHTTPRequestOperationManager *manager =[AFHTTPRequestOperationManager manager];
    manager.responseSerializer =[AFHTTPResponseSerializer serializer];
    [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //进行JSON格式的解析
        NSDictionary *dict =[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
//        NSLog(@"dict = %@",dict);
        NSArray *appList = dict[@"applications"];
        for(NSDictionary *appDict in appList){
           //这里先创建数据模型model
            [self creatModelCodeWithDictionary:appDict name:@"AppModel"];
           //kvc,如果键值没有对应的属性,会崩溃。
            AppModel *model =[[AppModel alloc] init];
            [model setValuesForKeysWithDictionary:appDict];
             model.desc =appDict[@"description"];
            [_dataArray addObject:model];
        }
         [_tableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
   
}

4.5 根据下载的数据分析,创建数据模型。

//创建model的代码,根据字典生成代码
-(void)creatModelCodeWithDictionary:(NSDictionary *)dict name :(NSString *)name
{
    printf("\n@interface %s:NSObject\n",name.UTF8String);
    for(NSString *key in dict){
    
        printf("@property(copy,nonatomic) NSString *%s;\n",key.UTF8String);
    }
    printf("@end\n");
}
============================================

@interface AppModel:NSObject
@property(copy,nonatomic) NSString *priceTrend;
@property(copy,nonatomic) NSString *expireDatetime;
@property(copy,nonatomic) NSString *fileSize;
@property(copy,nonatomic) NSString *categoryName;
@property(copy,nonatomic) NSString *ipa;
@property(copy,nonatomic) NSString *itunesUrl;
@property(copy,nonatomic) NSString *name;
@property(copy,nonatomic) NSString *version;
@property(copy,nonatomic) NSString *currentPrice;
@property(copy,nonatomic) NSString *starCurrent;
@property(copy,nonatomic) NSString *starOverall;
@property(copy,nonatomic) NSString *favorites;
@property(copy,nonatomic) NSString *iconUrl;
@property(copy,nonatomic) NSString *releaseNotes;
@property(copy,nonatomic) NSString *releaseDate;
@property(copy,nonatomic) NSString *updateDate;
@property(copy,nonatomic) NSString *slug;
@property(copy,nonatomic) NSString *downloads;
@property(copy,nonatomic) NSString *lastPrice;
@property(copy,nonatomic) NSString *ratingOverall;
@property(copy,nonatomic) NSString *applicationId;
@property(copy,nonatomic) NSString *desc;//改成了

@property(copy,nonatomic) NSNumber *categoryId;//修改
@property(copy,nonatomic) NSString *shares;//修改
@end

  4.6 创建UITableView ,展示数据

 

#pragma mark ---表格视图显示数据
-(void)createTableView
{

    _tableView  =[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.dataSource=self;
    _tableView.delegate =self;
    [self.view addSubview:_tableView];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *cellID =@"cell";
    AppCell *cell =[tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell ==nil) {
        cell =[[AppCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
    }
    AppModel *model =_dataArray[indexPath.row];
//    cell.textLabel.text =model.name;
    [cell.iconImageView setImageWithURL:[NSURL URLWithString:model.iconUrl]];
//    cell.nameLable.text =model.name;
    cell.nameLable.text =[NSString stringWithFormat:@"%d.%@",indexPath.row+1,model.name];
    
//    cell.priceLabel.text =model.lastPrice;
    cell.priceLabel.text =[NSString stringWithFormat:@"¥%.2f",model.lastPrice.doubleValue];
    cell.categoryLabel.text =model.categoryName;
    [cell.starView setStar:model.starCurrent.doubleValue];
    cell.shareLabel.text =[NSString stringWithFormat:@"分享:%d",model.shares.intValue];
    cell.favoriteLabel.text =[NSString stringWithFormat:@"收藏:%d",model.favorites.intValue];
    cell.downloadLabel.text =[NSString stringWithFormat:@"下载:%d",model.downloads.intValue];
    return cell;
    
}

 

IOS项目之 爱限免一

标签:

原文地址:http://www.cnblogs.com/jayzhang/p/4403467.html

(0)
(1)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!