Believe in your own future, will thank yourself right now Sinner Yun

Sinner_Yun

界面,数据下载

 

 

//  MyTabBar.m

 

 

#import "MyTabBar.h"

#import "LimitViewController.h"

#import "SellViewController.h"

#import "FreeViewController.h"

#import "SabojectViewController.h"

#import "HotViewController.h"

@interface MyTabBar ()

 

@end

 

@implementation MyTabBar

 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}

 

- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    NSArray *vcArr=@[@"LimitViewController",@"SellViewController",@"FreeViewController",@"SabojectViewController",@"HotViewController"];

    

    NSArray *imgArr=@[@"tabbar_limitfree",@"tabbar_reduceprice",@"tabbar_appfree",@"tabbar_subject",@"tabbar_rank"];

    

    NSArray *titleArr=@[@"限免",@"降价",@"免费",@"专题",@"热榜"];

    

    NSMutableArray *ncArr=[NSMutableArray array];

    

    for (int i=0; i<5; i++) {

        

        //将字符串转化成类

        Class cl =NSClassFromString(vcArr[i]);

        

        //父类指针指向子类对象

        UIViewController *vc=[[cl alloc]init];

        

        //放到导航里

        UINavigationController *nc=[[UINavigationController alloc]initWithRootViewController:vc];

        vc.navigationItem.title=titleArr[i];

        

        //tabBar上显示的专用按钮

        UITabBarItem *item=[[UITabBarItem alloc]initWithTitle:titleArr[i] image:[UIImage imageNamed:imgArr[i] ]tag:0];

        nc.tabBarItem=item;

        

        [ncArr addObject:nc];

        

        

    }

    

    self.viewControllers=ncArr;

    

}

 

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

 

 

 

 

 

 

 

 

 

 

 

 

//  MyConnection.h

 

 

#import <Foundation/Foundation.h>

 

//前向声明(告诉编译器MyConnection是一个类)

@class MyConnection;

 

@protocol MyConnectionDelegate <NSObject>

 

//下载成功以后会调

-(void)myConnectionDidFinish:(MyConnection *)mc;

 

//下载失败以后会调

-(void)myConnection:(MyConnection *)mc didFailWithError:(NSError *)error;

 

@end

 

@interface MyConnection : NSObject <NSURLConnectionDataDelegate>

{

    //网络请求

    NSURLRequest *_request;

}

 

//用来接收下载的数据

@property(nonatomic,retain)NSMutableData *responseData;

 

//代理指针

@property(nonatomic,assign)id <MyConnectionDelegate>delegate;

 

//接收一个字符串作为网址

-(id)initWithUrlStr:(NSString *)urlStr;

 

//开始异步下载

-(void)starAsynchronous;

 

//类方法创建对象

+(MyConnection *)connectionWithUrlStr:(NSString *)urlStr delegate:(id<MyConnectionDelegate>)delegate;

 

@end

 

 

 

 

 

 

 

 

 

 

 

 

 

//  MyConnection.m

 

 

#import "MyConnection.h"

 

@implementation MyConnection

 

 

-(id)initWithUrlStr:(NSString *)urlStr

{

    if (self=[super init]) {

        _request=[NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]cachePolicy:0 timeoutInterval:15];

        

        _responseData=[[NSMutableData alloc]init];

        

        NSLog(@"urlStr==%@",urlStr);

    }

    return self;

}

 

 

-(void)starAsynchronous

{

    [NSURLConnection connectionWithRequest:_request delegate:self];

    

    [UIApplication sharedApplication].networkActivityIndicatorVisible=YES;

    

}

 

 

+(MyConnection *)connectionWithUrlStr:(NSString *)urlStr delegate:(id<MyConnectionDelegate>)delegate

{

    MyConnection *mc=[[MyConnection alloc]initWithUrlStr:urlStr];

    mc.delegate=delegate;

    return mc;

}

#pragma mark - NSURLConnection

 

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

{

    _responseData.length=0;

}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

{

    [_responseData appendData:data];

}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection

{

    //下载成功以后,调用代理的方法,传一个参数过去,参数就是用来下载的对象自己

    [self.delegate myConnectionDidFinish:self];

}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

{

    //下载失败,告诉代理

    [self.delegate myConnection:self didFailWithError:error];

}

 

-(void)dealloc

{

    [UIApplication sharedApplication].networkActivityIndicatorVisible=NO;

}

 

@end

 

 

 

 

 

 

 

 

 

 

 

 

 

//  AppModel.h

 

 

#import <Foundation/Foundation.h>

 

@interface AppModel : NSObject

 

 

@property (nonatomic, copy) NSString  *applicationId;

@property (nonatomic, copy) NSString  *iconUrl;

@property (nonatomic, copy) NSString  *name;

@property (nonatomic, copy) NSString  *expireDatetime;

@property (nonatomic, copy) NSString  *categoryName;

@property (nonatomic, copy) NSString  *starCurrent;

@property (nonatomic, copy) NSString  *lastPrice;

@property (nonatomic, copy) NSString  *other;

 

 

@end

 

 

 

 

 

 

 

 

 

 

 

 

//  AppCell.h

 

 

#import <UIKit/UIKit.h>

 

@interface AppCell : UITableViewCell

 

@property (weak, nonatomic) IBOutlet UIImageView *iconView;

 

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;

 

@property (weak, nonatomic) IBOutlet UILabel *timeLabel;

 

@property (weak, nonatomic) IBOutlet UIImageView *starView;

 

@property (weak, nonatomic) IBOutlet UILabel *priceLabel;

 

@property (weak, nonatomic) IBOutlet UILabel *categoryLabel;

 

@property (weak, nonatomic) IBOutlet UILabel *otherLabel;

 

 

@end

 

 

 

 

 

 

//  LimitViewController.m

 

 

#import "LimitViewController.h"

#import "MyConnection.h"

#import "AppCell.h"

#import "AppModel.h"

#import "UIImageView+WebCache.h"

@interface LimitViewController ()<MyConnectionDelegate,UITableViewDataSource,UITableViewDelegate>

{

    NSMutableArray *_dataArr;

    UITableView *_myTableView;

}

 

@end

 

@implementation LimitViewController

 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}

 

- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    _dataArr=[[NSMutableArray alloc]init];

    

    NSString *urlStr=@"http://iappfree.candou.com:8080/free/applications/limited?currency=rmb&page=9";

    

    MyConnection *mc=[[MyConnection alloc]initWithUrlStr:urlStr];

    mc.delegate=self;

    [mc starAsynchronous];

    

    

    _myTableView=[[UITableView alloc]initWithFrame:self.view.bounds];

    _myTableView.delegate=self;

    _myTableView.dataSource=self;

    [self.view addSubview:_myTableView];

    

    

    

}

 

#pragma mark - myConnection

-(void)myConnectionDidFinish:(MyConnection *)mc

{

    NSDictionary *dataDic=[NSJSONSerialization JSONObjectWithData:mc.responseData options:0 error:nil];

    

    

    //数据下载完成以后封装到数据模型里,然后将数据模型放到数据源中

    for (NSDictionary *dic in [dataDic objectForKey:@"applications"]) {

        AppModel *am=[[AppModel alloc]init];

        am.applicationId=[dic objectForKey:@"applicationId"];

        am.iconUrl=[dic objectForKey:@"iconUrl"];

        am.name=[dic objectForKey:@"name"];

        am.expireDatetime=[dic objectForKey:@"expireDatetime"];

        am.categoryName=[dic objectForKey:@"categoryName"];

        am.lastPrice=[NSString stringWithFormat:@"¥ %@",[dic objectForKey:@"lastPrice"]];

        am.starCurrent=[dic objectForKey:@"starCurrent"];

        am.other=[NSString stringWithFormat:@"收藏:%@ 次  分享:%@ 次  下载:%@ 次",[dic objectForKey:@"favorites"],[dic objectForKey:@"shares"],[dic objectForKey:@"downloads"]];

        [_dataArr addObject:am];

    }

    [_myTableView reloadData];

}

 

-(void)myConnection:(MyConnection *)mc didFailWithError:(NSError *)error

{

    NSLog(@"AAAAA");

}

 

 

 

#pragma mark - tableView

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return _dataArr.count;

}

 

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return 100;

}

 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    

    AppCell *cell=[tableView dequeueReusableCellWithIdentifier:@"qqq"];

    if (!cell) {

        cell=[[[NSBundle mainBundle]loadNibNamed:@"AppCell" owner:self options:nil]lastObject];

        

        UIImageView *iv=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 100)];

        cell.backgroundView=iv;

        

        

        cell.starView.clipsToBounds=YES;

        cell.starView.contentMode=UIViewContentModeLeft;

        

    }

    

    UIImageView *iv=(UIImageView *)cell.backgroundView;

    if (indexPath.row%2) {

        iv.image=[UIImage imageNamed:@"cate_list_bg1"];

    } else {

        iv.image=[UIImage imageNamed:@"cate_list_bg2"];

    }

    

    AppModel *am=[_dataArr objectAtIndex:indexPath.row];

    

    [cell.iconView setImageWithURL:[NSURL URLWithString:am.iconUrl]];

    cell.nameLabel.text=am.name;

    cell.categoryLabel.text=am.categoryName;

    cell.otherLabel.text=am.other;

    cell.priceLabel.text=am.lastPrice;

    

    NSLog(@"===%@",am.expireDatetime);

    cell.timeLabel.text=[NSString stringWithFormat:@"剩余:%@",[self subTimeWithDateString:am.expireDatetime]];

    

    

    CGRect rect=cell.starView.frame;

    rect.size.width=65/5*am.starCurrent.floatValue;

    cell.starView.frame=rect;

    

    

    return cell;

}

 

-(NSString *)subTimeWithDateString:(NSString *)dateString

{

    NSDateFormatter *df=[[NSDateFormatter alloc]init];

    df.dateFormat=@"yyyy-MM-dd HH:mm:ss.0";

    NSDate *date=[df dateFromString:dateString];

    

    //计算一个时间和现在时间的间隔(秒)

    int subTime=[date timeIntervalSinceNow];

    

    return [NSString stringWithFormat:@"%02d:%02d:%02d",subTime/3600,subTime%3600/60,subTime%3600%60];

    

}

 

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

 

 

 

 

posted on 2014-04-02 20:57  Sinner_Yun  阅读(290)  评论(0编辑  收藏  举报

导航